Html helper @Html.Pager
from MvcPaging 2.0. has .Options(o => o.RouteValues(object RouteValues))
which can return Model back to Controller,but MvcPaging requires this helper to be filled with IPagedList<model>
in View that he lives in. This is the Model that generates table and paging. What is the best way to implement mvcpaging 2.0. using SearchModel for 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 model for search?Or is there another way, a better way, to have complex searches and not using model to transfer data query? Any thoughts?
I am using MvcPaging 2.0. ,docs
EDIT:*
Thanks Darin for answer but I manage to pull it of 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()));
}
You could write a custom extension method that will harvest all query string parameters and add them to the page link in addition to all the currentPage, pageNumber, totalItemCount, etc ... stuff:
public static class PagerOptionsBuilderExtensions
{
public static PagerOptionsBuilder AddFromQueryString(
this PagerOptionsBuilder builder,
HttpRequestBase request
)
{
foreach (string item in request.QueryString)
{
builder.AddRouteValue(item, request.QueryString[item]);
}
return builder;
}
}
and then:
.Options(o => o.RouteValues(Model).AddFromQueryString(Request))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With