views:

14

answers:

1

Im showing a table of data using a paginated list, with a search box and submit button above this. I wish to be able to search this table of data then re-Post the view and update with the newly searched for data on the click of the submit button. How would I do this in MVC? Would I have to start looking at AJAX or JQuery or can it be done using the built in GET and POST techniques?

Sorry if the question doesn't make a whole load of sense, I'm new here and to MVC :D

A: 

The basic pattern is the same whether it's AJAX or not. You would have 2 actions for your view, a GET and a POST, your search form should be a simple ViewModel with the fields you'd search/sort on that also includes your pagination.

[HttpGet]
public ActionResult DataTable(int? page){
    var data = myRepository.GetData(page);
    return View(data);
}

[HttpPost]
public ActionResult DataTable(int? page, SearchModel search){
    var data = myRepository.GetSearchedData(page, search);
    return View(data);
}

If you were doing it via AJAX the difference is that you'd have the data display in a PartialView, your DataTable View would render this partial within a named div, the HttpPost method would return a PartialView and you'd replace the named div's contents with this result (JQuery's $.load() method would be the easiest way to do this).

Chao