0

我有一个 mvcjqgrid:

@(Html.Grid("dataGrid")
          .SetJsonReader(new MvcJqGrid.DataReaders.JsonReader { Id = "Id", RepeatItems = false })
          .SetRequestType(RequestType.Post)
          .AddColumn(new Column("Name").SetLabel("Name").SetSearch(true))
          .AddColumn(new Column("Email").SetLabel("E-Mail").SetSearch(true).SetFormatter(Formatters.Email))
          .AddColumn(new Column("Phone").SetLabel("Phone").SetSearch(true))
          .SetSearchToolbar(true)
          .SetUrl(Url.Action("GetData", "Controller"))
          .SetSearchOnEnter(false)
          .SetRowNum(10)
          .SetRowList(new[] { 10, 15, 20, 50 })
          .SetViewRecords(true)
          .SetPager("pager"))

和控制器:

    public ActionResult GetData()
    {
        return View(new myEntity[0]);
    }

    [HttpPost]
    public JsonResult GetData(GridSettings gridSettings)
    {
        int totalRecords = DataHelper.GetCount();
        var data = DataHelper.GetData(gridSettings);
        var jsonData = new
        {
            total = totalRecords / gridSettings.PageSize + 1,
            page = gridSettings.PageIndex,
            records = totalRecords,
            rows = data
        };
        return Json(jsonData);
    }

编辑:
所以我的问题是如何以正确的方式将 GridSettings 存储在会话中,我需要每次用户返回此页面时,页面应该与他离开时相同?
如果我做:

Session["settings"] = gridSettings;

我需要一些方法来比较存储的 gridSettings 和传递给 action 的那个。

4

2 回答 2

0

答案是重新创建 Grid:

@{
     var setting = Session["settings"] as GridSettings;
}
@(Html.Grid("dataGrid")
          .SetJsonReader(new MvcJqGrid.DataReaders.JsonReader { Id = "Id", RepeatItems = false })
          .SetRequestType(RequestType.Post)
          .AddColumn(new Column("Name").SetLabel("Name").SetSearch(true))
          .AddColumn(new Column("Email").SetLabel("E-Mail").SetSearch(true).SetFormatter(Formatters.Email))
          .AddColumn(new Column("Phone").SetLabel("Phone").SetSearch(true))
          .SetSearchToolbar(true)
          .SetUrl(Url.Action("GetData", "Controller"))
          .SetSearchOnEnter(false)
          .SetRowNum(setting != null?setting.PageSize : 10)
          .SetPage(setting != null?setting.PageIndex : 1);
          .SetSortName(setting != null?setting.SortColumn : "");
          .SetRowList(new[] { 10, 15, 20, 50 })
          .SetViewRecords(true)
          .SetPager("pager"))
于 2013-05-15T08:18:47.667 回答
0

在这种情况下,为什么不使用Http Cache呢?我们可以编写一个缓存提供程序,而 Http Cache 就是其中的一种实现方式。因此,将来您可以为其扩展更多提供程序。

于 2013-05-13T11:06:53.217 回答