سؤال

I have a 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"))

and controller:

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

EDIT:
so my question is how can i store GridSettings in session in right way, i need every time user backs to this page, page should be the same as when he left?
If i do:

Session["settings"] = gridSettings;

i need some way to compare stored gridSettings with the one passed to action.

هل كانت مفيدة؟

المحلول 2

The answer is to recreate 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"))

نصائح أخرى

Why don't you use the Http Cache for this case? We can write a caching provider, and Http Cache is one implementation for this. So in the future you can extend more providers for it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top