如何在MVC3中使用MvcPaging2.0使用RouteValues?

时间:2012-08-01 18:16:34

标签: c# asp.net .net asp.net-mvc-3 paging

来自 MvcPaging 2.0。的Html助手@Html.Pager.Options(o => o.RouteValues(object RouteValues)),可以将Model返回给Controller,但是MvcPaging要求此助手填充IPagedList<model> in查看他的生活。这是生成表和分页的模型。实现mvcpaging 2.0的最佳方法是什么?使用SearchModel进行搜索,使用Model来显示结果?

示例:

模型:

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>

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()));
}

问题是如何使用模型进行搜索来实现MvcPaging2.0?还是有另一种方法,更好的方法来进行复杂搜索而不使用模型来传输数据查询?有什么想法吗?

我正在使用MvcPaging 2.0.docs

修改:*

感谢Darin的回答,但我设法将其拉出来:

* _ * 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()));
}

1 个答案:

答案 0 :(得分:5)

你可以编写一个自定义扩展方法,它将获取所有查询字符串参数,并将它们添加到页面链接以及所有currentPage,pageNumber,totalItemCount等...东西:

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;
    }
}

然后:

.Options(o => o.RouteValues(Model).AddFromQueryString(Request))