@Html.Pager
来自MvcPaging 2.0的Html 助手。有.Options(o => o.RouteValues(object RouteValues))
它可以将Model返回给Controller,但是MvcPaging需要在他所在的View中填充这个helper IPagedList<model>
。这是生成表和分页的Model。实现 mvcpaging 2.0 的最佳方法是什么?使用 SearchModel 进行搜索并使用模型显示结果?
例子:
楷模:
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; }
}
查看: 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>
控制器
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()));
}
问题是如何实现 MvcPaging2.0 使用模型进行搜索?或者是否有另一种方式,更好的方式来进行复杂的搜索而不使用模型来传输数据查询?有什么想法吗?
我正在使用MvcPaging 2.0。,文档
编辑:*
感谢达林的回答,但我设法把它拉成这样:
*_ResultPartial.cshtml*
@Html.Pager(Model.PageSize, Model.PageNumber,
Model.TotalItemCount, new AjaxOptions
{
UpdateTargetId = "main_search_result_table_id"
}).Options(o => o.Action("AjaxPaging"))
控制器
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()));
}