Html helper @Html.Pager from MvcPage 2.0. has .Options(o => o.RouteValues(object RouteValues)) that can return the model back to the controller, but MvcPaging requires that this helper be populated with the IPagedList<model> in the view it lives in. This is the Model that generates the table and paging. What is the best way to implement mvcpaging 2.0. using SearchModel to search and Model to display results?
Example:
MODELS:
public class SearchModel { public string FirstName { get; set; } public string LastName { get; set; } } public class Person { [Key] public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Dob { get; set; } public string City { get; set; } }
VIEW: Index.cshtml
@using (Ajax.BeginForm("Search", "SearchPerson", new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "main_search_result_table_id" })) { @Html.TextBoxFor(m => m.FirstName) @Html.TextBoxFor(m => m.LastName) <input type="submit" value="Search"/> } <div id="main_search_result_table_id"> @{Html.RenderPartial("_InitPartialEmpty");} </div>
_ResultPartial.cshtml
@using MvcPaging @model IPagedList<Models.Person> <table> @foreach (var p in Model) { <tr> <td>@p.FirstName</td> <td>@p.LastName</td> <td>@p.Dob</td> <td>@p.City</td> </tr> } <table> @Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new AjaxOptions { UpdateTargetId = "main_search_result_table_id" }).Options(o => o.RouteValues(Model)) //==> IPagedList<Models.Person>
CONTROLLER
public ActionResult SearchPerson(int? page,SearchModel person) { List<Person> result= adapter.GetPersons(person); int currentPageIndex = page.HasValue ? page.Value - 1 : 0; return PartialView("_ResultPartial", result.ToPagedList(currentPageIndex, 10, result.Count())); }
The question is how to implement MvcPaging2.0 using the search model? Or is there another way, a better way, to have a complex search and not use a model to transmit a data request? Any thoughts?
I am using MvcPaging 2.0. docs
EDIT: *
Thanks to Darin for the answer, but I manage to do it like this:
* _ * ResultPartial.cshtml
@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new AjaxOptions { UpdateTargetId = "main_search_result_table_id" }).Options(o => o.Action("AjaxPaging"))
CONTROLLER
public ActionResult SearchPerson(int? page,SearchModel person) { IQueryable<Person> query= adapter.GetPersons(person); Session["SearchQuery"] = query; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; List<Person> persons = query.ToList(); return PartialView("_ResultPartial", persons.ToPagedList(currentPageIndex, 10, persons.Count())); } public ActionResult AjaxPaging(int? page) { IQueryable<Person> query = Session["SearchQuery"] as IQueryable<Person>; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; List<Person> persons = query.ToList(); return PartialView("_ResultPartial", persons.ToPagedList(currentPageIndex, 10, persons.Count())); }