I'm creating a page with a table that is populated (using jQuery) from a database. I'm trying to figure out the best way to search the table (or I guess the correct word is "filter"). I have a "partial view" .cshtml file that creates a search bar inside the file where the table is. My question is how can I pass a variable (which will be the only thing inside a text box when the search button is pressed) from the partial view file to the "actual" view file so I can use it as the parameter for the filter? I've done lots of searching about this online and can't find exactly what I'm looking for. Thanks in advance for your help!
-
Check out this api.jquery.com/jquery.get . You can call your controller and get your partial view. You can pass your filter variables with it too, to get the correct response.mattfetz– mattfetz2015-02-04 19:02:15 +00:00Commented Feb 4, 2015 at 19:02
-
@mattfetz okay, so if I have the file "StudentAllotment.cshtml", and the partial view is in file "_StudentSearchBar.cshtml", and I'm getting the variable "filtering" from the StudentSearchBar file (which holds the desired filtering parameters), I would call: $.get("~/Views/StudentAllotments/Allotments.cshtml", {filtering}); From the StudentSearchBar file after I populate "filtering", but how would I then use the variable in the other file? Thanks!Josiah Nusbaum– Josiah Nusbaum2015-02-04 19:40:39 +00:00Commented Feb 4, 2015 at 19:40
-
I gave a example of what you should do to get a partial in MVC.mattfetz– mattfetz2015-02-04 20:00:42 +00:00Commented Feb 4, 2015 at 20:00
Add a comment
|
1 Answer
Jquery
$(document).ready(function(){
$.get("~/Student/GetStudent?id=1", function(html){
//append html
})
})
C#
Lets say this get method is in a controller called Student
[HttpGet]
public ActionResult GetStudent(int id)
{
//use the paremeter in this case id to create a model with the correct data
//_StudentPartial being the partial html and model being the model that populates the partial
return PartialView("_StudentPartial",model);
}
8 Comments
mattfetz
@StephenMuecke thats the way I do it too, but for learning purposes I wrote out the entire url with parems in it for easier understanding.
Josiah Nusbaum
@mattfetz I'm not sure if this is exactly what I'm looking for. I want to be able to pass the variable from the partial view to the main view when a button in the partial view is pressed. Is there a good way to do that?
mattfetz
@JosiahNusbaum Well you can have a jquery click listener on the button, get the value from the partial and append the value in a hidden input somewhere else in the html where you want it. But partial view is just a html page that you are inserting to another html. Once you add the partial to the page it essential is part of the main page.
|