在剃刀查看设置分页的页面大小的DropDownList [英] Set paging page size DropDownList in razor View

查看:171
本文介绍了在剃刀查看设置分页的页面大小的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使我的as​​p.net web表单页面大小选择的下拉列表中。其实我做它已经,但是当我改变页面大小从我的下拉列表中有任何改变。我想我需要编写额外的东西在我的控制器,或者也许我的观点是错误的?

我Index.cshtml的看法:

  @model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using(Html.BeginForm(指数,用户))
{
    &所述p为H.;
        按名称查找:@ Html.TextBox(SearchString在ViewBag.CurrentFilter为字符串)
        <输入类型=提交级=BTN BTN-主要值=搜索/>
    &所述; / P>
}
<表类=表>
    &所述; TR>
        <第i @ Html.ActionLink(登录,索引,新的{中将sortOrder = ViewBag.LoginSortParm,currentFilter = ViewBag.CurrentFilter})LT; /第i
    < / TR>
    @foreach(在Model.Model2 VAR项){
        &所述; TR>
            < TD> @ Html.DisplayFor(modelItem => item.Login)LT; / TD>
        < / TR>
    }
< /表>页@(Model.Model2.PageCount< Model.Model2.PageNumber 0:Model.Model2.PageNumber)的@ Model.Model2.PageCount
@ Html.PagedListPager(Model.Model2,页=> Url.Action(指数,
    新{页面中将sortOrder = ViewBag.CurrentSort,currentFilter = ViewBag.CurrentFilter}))选择页面大小@ Html.DropDownList(每页,新的SelectList(新字典<字符串,整数> {{10,10},{20,20},{100,100}},关键,值,Model.Model2.Count))

这是我的控制器。在这里,关于DropDownList的我没有任何补充:

 公众的ActionResult指数(字符串中将sortOrder,串currentFilter,字符串搜索字符串,诠释?页)
{
    ViewBag.CurrentSort =中将sortOrder;
    如果(搜索字符串!= NULL)
    {
        页面= 1;
    }
    其他
    {
        搜索字符串= currentFilter;
    }
            ViewBag.CurrentFilter =搜索字符串;
    在db.Users.Include从s VAR用户=(U => u.CustomerProject).INCLUDE(U => u.Service).INCLUDE(U => u.Customer)选择S;
    INT的pageSize = 20;
    INT PAGENUMBER =(第53页1);
    VAR研究所=新TimeReportViewMod();
    inst.Model1 = users.ToList();
    inst.Model2 = users.ToPagedList(PAGENUMBER,pageSize的);
    返回视图(指数,研究所);
}


解决方案

您需要包括在你的DropDownList的<形式> 标记,这样它的价值被提交到控制器的方法,并且该方法还需要一个额外的参数绑定到值。您还需要在所选择的值 Html.PagedListPager(),以便其当你去到另一个网页保留。

您还没有表现出你的 TimeReportViewMod 模式,但它的外径,它包含的属性的IEnumerable<使用者> IPagedList<使用者> ,这也是的IEnumerable<使用者> 所以第一个是不必要的。但是,它应该包含的是你用于搜索和过滤的属性,这样就可以强烈地绑定到你的属性,避免使用 ViewBag 的。它应该看起来像

 公共类TimeReportVM
{
    [显示(NAME =按名称查找)]
    公共字符串SearchString在{搞定;组; }
    公共字符串SortOrder的{搞定;组; }
    [显示(名称=选择页面大小)]
    公众诠释每页{搞定;组; }
    公众的SelectList PageSizeList {搞定;组; }
    公共IPagedList<使用者>用户{搞定;组; }
}

请注意:它不清楚是什么中将sortOrder 是 - 你从来没有在任何地方使用它。也不是你曾经使用过搜索字符串 / currentFilter 在您查询的价值。

然后你控制器的方法是

 公众的ActionResult指数(字符串中将sortOrder,字符串搜索字符串,INT的pageSize,诠释?页)
{
    如果(搜索字符串!= NULL)
    {
        页面= 1;
    }
    //这个查询应该考虑中将sortOrder和搜索字符串的值?
    在db.Users.Include从s VAR用户=(U => u.CustomerProject).INCLUDE(U => u.Service).INCLUDE(U => u.Customer)选择S;
    TimeReportVM模式=新TimeReportVM()
    {
        SortOrder的=中将sortOrder,
        SearchString在=搜索字符串,
        每页= pageSize的,
        PageSizeList =新的SelectList(新INT [] {10,20,100}),
        用户= users.ToPagedList(第53页1,pageSize的);
    };
    返回查看(模型);
}

和视图

  @model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using(Html.BeginForm())
{
    @ Html.LabelFor(M = GT; m.SearchString)
    @ Html.TextBoxFor(M = GT; m.SearchString)
    @ Html.LabelFor(M = GT; m.PageSize)
    @ Html.DropDownListFor(M = GT; m.PageSize,Model.PageSizeList)
    <输入类型=提交级=BTN BTN-主要值=搜索/>
}
....
页@(Model.Users.PageCount< Model.Users.PageNumber 0:Model.Users.PageNumber)的@ Model.Users.PageCount
@ Html.PagedListPager(Model.Users,页=> Url.Action(指数,
    新{页面中将sortOrder = Model.SortOrder,搜索字符串= Model.SearchString,pageSize的= Model.PageSize}))

I would like to make in my asp.net web form page size selection with drop down list. Actually I made it already, but when I change page size from my drop down list there isn't change anything. I guess I need to write something extra in my controller, or maybe my View is wrong?

My Index.cshtml view:

@model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using (Html.BeginForm("Index", "Users"))
{
    <p>
        Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
        <input type="submit"  class="btn btn-primary" value="Search" />
    </p>
}
<table class="table">
    <tr>
        <th>@Html.ActionLink("Login","Index", new { sortOrder = ViewBag.LoginSortParm, currentFilter = ViewBag.CurrentFilter })</th>
    </tr>
    @foreach (var item in Model.Model2) {
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Login)</td>
        </tr>
    }
</table>

Page @(Model.Model2.PageCount < Model.Model2.PageNumber ? 0 : Model.Model2.PageNumber) of @Model.Model2.PageCount
@Html.PagedListPager(Model.Model2, page => Url.Action("Index", 
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }) )

Select page size @Html.DropDownList("PageSize", new SelectList(new Dictionary<string, int> { { "10", 10 }, { "20", 20 }, { "100", 100 } }, "Key", "Value", Model.Model2.Count))

And this is my controller. Here I didn't added anything regarding DropDownList :

public ActionResult Index(string sortOrder, string currentFilter, string searchString,  int? page)
{
    ViewBag.CurrentSort = sortOrder;
    if (searchString != null)
    {
        page = 1;
    }
    else
    {
        searchString = currentFilter;
    }
            ViewBag.CurrentFilter = searchString;
    var users = from s in db.Users.Include(u => u.CustomerProject).Include(u => u.Service).Include(u => u.Customer) select s;
    int pageSize = 20;
    int pageNumber = (page ?? 1);
    var inst = new TimeReportViewMod();
    inst.Model1 = users.ToList();
    inst.Model2 = users.ToPagedList(pageNumber, pageSize);
    return View("Index", inst);
}

解决方案

You need to include the dropdownlist within you <form> tags so its value is submitted to the controller method, and that method also needs an additional parameter to bind to the value. You also need to include the selected value in the Html.PagedListPager() so that its retained when you go to another page.

You have not shown your TimeReportViewMod model, but its od that it contains properties IEnumerable<User> and IPagedList<User>, which is also IEnumerable<User> so the first one is unnecessary. But what it should contain is the properties you use for searching and filtering so that you can strongly bind to your properties and avoid the use of ViewBag. It should look like

public class TimeReportVM
{
    [Display(Name = "Find by name")]
    public string SearchString { get; set; }
    public string SortOrder { get; set; }
    [Display(Name = "Select page size")]
    public int PageSize { get; set; }
    public SelectList PageSizeList { get; set; }
    public IPagedList<User> Users { get; set; }
}

Note: Its not clear what sortOrder is for - you never use it anywhere. Nor do you ever use the value of searchString/currentFilter in you query.

Then you controller method is

public ActionResult Index(string sortOrder, string searchString, int pageSize, int? page)
{
    if (searchString != null)
    {
        page = 1;
    }
    // this query should be taking into account the values of sortOrder and searchString?
    var users = from s in db.Users.Include(u => u.CustomerProject).Include(u => u.Service).Include(u => u.Customer) select s;
    TimeReportVM model = new TimeReportVM()
    {
        SortOrder = sortOrder,
        SearchString = searchString,
        PageSize = pageSize,
        PageSizeList = new SelectList(new int[]{ 10, 20, 100 }),
        Users = users.ToPagedList(page ?? 1, pageSize);
    };
    return View(model);
}

and in the view

@model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.SearchString)
    @Html.TextBoxFor(m => m.SearchString)
    @Html.LabelFor(m => m.PageSize)
    @Html.DropDownListFor(m => m.PageSize, Model.PageSizeList)
    <input type="submit"  class="btn btn-primary" value="Search" />
}
....
Page @(Model.Users.PageCount < Model.Users.PageNumber ? 0 : Model.Users.PageNumber) of @Model.Users.PageCount
@Html.PagedListPager(Model.Users, page => Url.Action("Index", 
    new { page, sortOrder = Model.SortOrder, searchString = Model.SearchString, pageSize = Model.PageSize }) )

这篇关于在剃刀查看设置分页的页面大小的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆