如何在 Grid.mvc 中更改页面大小

How to change page size in Grid.mvc

我有以下控制器:

public ActionResult Grid()
        {
            schoolEntities db = new schoolEntities();
            List<Student> result = db.Students.ToList();
            // I can't use pagesizelist here, taken from the view
            ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
            return View(result);
        }

及相关观点:

...
@Html.DropDownList("Page", new SelectList(new Dictionary<string, int> { { "10", 10 }, { "20", 20 }, { "50", 50 } }, "Key", "Value"), new { id = "pagesizelist" })

<div class="code-cut">
    @Html.Grid(Model).Columns(Columns =>
{
    Columns.Add(c => c.StudentID).Titled("Id").Filterable(true);
    Columns.Add(c => c.LastName).Titled("Last name").Filterable(true);
    Columns.Add(c => c.FirstName).Titled("First name").Filterable(true);
    Columns.Add(c => c.EnrollmentDate).Titled("Enrollment date").Filterable(true);
    Columns.Add()
...
}).WithPaging(ViewBag.pageSize).Sortable(true)

我想根据 DropDownList 的变化以某种方式动态设置 .WithPaging() 参数。

  • 将 "Page" ddl 包装在一个表单中,

  • 订阅其客户端"onchange"。在那里提交表格,

  • 在单独的操作方法中处理 "change size" 操作,

  • 指定新的页面大小值并重新加载整个视图:

查看:

@model IEnumerable<Student>
@using GridMvc.Html

<script type="text/javascript">
    function onDdlPageChange(sender) {
        $("#formIdHere").submit();
    }
</script>

@using (Html.BeginForm("Grid", "Home", FormMethod.Post, new { id = "formIdHere" }))
{
    @Html.DropDownList("Page", new SelectList(new Dictionary<string, int> { { "10", 10 }, { "20", 20 }, { "50", 50 } }, "Key", "Value", ViewBag.pageSize), new { id = "pagesizelist", onchange = "onDdlPageChange(this);" })

    @Html.Grid(Model).Columns(Columns =>
    {
        Columns.Add(c => c.StudentID).Titled("Id").Filterable(true);
        Columns.Add(c => c.LastName).Titled("Last name").Filterable(true);
        Columns.Add(c => c.FirstName).Titled("First name").Filterable(true);
        Columns.Add(c => c.EnrollmentDate).Titled("Enrollment date").Filterable(true);
        //Columns.Add();
    }).WithPaging(ViewBag.pageSize).Sortable(true)
}

控制器:

public class HomeController : Controller
{
    public static readonly string viewNameWithGrid = "Grid";
    public static readonly int defaultPageSize = 10;

    private static readonly string SavedPageSizeSessionKey = "PageSizeKey";
    public int SavedPageSize
    {
        get
        {
            if (Session[SavedPageSizeSessionKey] == null)
                Session[SavedPageSizeSessionKey] = defaultPageSize;
            return (int)Session[SavedPageSizeSessionKey];
        }
        set { Session[SavedPageSizeSessionKey] = value; }
    }

    //The same as the Action name
    //return View(result);

    //Initial Load
    [HttpGet]
    public ActionResult Grid()
    {
        return GetViewWithGrid(SavedPageSize);
    }

    //Change Page Size
    [HttpPost]
    public ActionResult Grid(int? Page)
    {
        if (Page.HasValue)
            SavedPageSize = Page.Value;
        //Page = DropDownList.id
        return GetViewWithGrid(SavedPageSize);
    }

    ActionResult GetViewWithGrid(int pageSize)
    {
        schoolEntities db = new schoolEntities();
        List<Student> result = db.Students.ToList();

        //ViewBag.pageSize = int.Parse(pagesizelist.SelectedValue);
        ViewBag.pageSize = pageSize;
        return View(viewNameWithGrid, result);
    }
}