1

我使用 MVC3 开发一个系统,并且要渲染 Grid,必须使用 WebGrid。我有一个 ActionResult 返回包含空网格的索引页面。我有另一个 ActionResult 接收这些过滤器参数,执行过滤器查询并返回填充的网格。当我对该网格进行分页时,该网格返回空,因为它调用了 Index ActionResult。

  • 所以我的问题是如何在分页 webgrid 时保留这些过滤结果?
4

2 回答 2

2

我找到了解决方案。它涉及将您的搜索保持在 Session(或 Cache 或 TempData)上,仅此而已。该解决方案很好,因为您可以使其适应您的实际情况,不需要特定的 JQuery 类。

魔术发生在您的 Index ActionResult(或您的默认 ActionResult,它将以其默认行为呈现网格页面)中。

代码示例:

[HttpGet]
public ActionResult Index()//My default action result that will render the grid at its default situation
{
    SearchViewModel model = new SearchViewModel(); 

    if (Request.IsAjaxRequest()) //First trick is here, this verification will tell you that someone sorted or paged the grid.
    {
        if (Session["SearchViewModel"] != null) //If session is not empty, you will get the last filtred values from it.
            model = (SearchViewModel)Session["Filtro"];
    }
    else // If it is not an AjaxRequest, you have to clear your Session, so new requests to Index with default behavior won't display filtred values.
    {
        Session["SearchViewModel"] = null;
    }

    model.GridResult = ExecuteFilter(model); // OPITIONAL! This code dependes on how is your real world situation. Just remember that you need to return a default behavior grid if the request was not called by the WebGrid, or return filtred results if WebGrid requested.
    return View(model);
}

因此,这将是您的默认 ActionResult。它将验证请求是否被 WebGrid 分页或排序事件调用,以决定是否返回过滤结果或正常行为结果。

下一步是搜索 POST ActionResult:

[HttpPost]
public ActionResult Index(SearchViewModel pesquisa) // IMPORTANT!! It is necessary to be the SAME NAME of your GET ActionResult. The reason for that I know, but won't discuss here because it goes out of the question.
{
    SearchViewModel model = new SearchViewModel();
    model.GridResult = ExecuteFilter(pesquisa); // Execute your filter
    Session["SearchViewModel"] = model; //Save your filter parameters on Session.
    return View("Index", model);
}

就是这样。Index.cshtml 没有任何技巧。只是 ActionResult 索引的 SearchForm,将我的 SearchViewModel 作为参数传递。

为什么这个解决方案有效?

好吧,当您单击排序或分页时,WebGrid 会执行类似于此的 JavaScript:

$('#yourGrid').load('它传递用于显示当前页面的url,以及一些分页或排序参数,但这些参数是WebGrid使用的') 由于它执行了.load() 方法,因此请求将是一个 GET,并且会命中您的 Index GET ActionResult。但这是一个 AJAX 调用,所以我们的魔术将使用您保存在 Session 上的参数再次执行过滤器。

我提醒的唯一细节是关于您的默认网格行为。GET Index ActionResult 必须永远返回一个有效的网格结果,不管它是否在 Session 上有过滤器。

于 2013-03-20T15:43:41.783 回答
0

这应该可以帮助你。 链接 本文解释了如何进行分页并允许您保持数据源相同。

此外,通常当我执行过滤器时,我会在 javascript 中使用过滤器的每个模型字段创建一个对象,然后在网格执行它的 DataBinding 事件时将其传递给它。我主要与 Telerik 合作,所以我对 Webgrids 不太熟悉,否则我会给你举个例子。这基本上就是我所做的。

看法

function onDataBinding(e) {
    var searchModel = {
        SearchName: $('#@Html.FieldIdFor(model => model.SearchName")').val(),
        ...
    };
    e.data = searchModel;
}

控制器

    [GridAction(EnableCustomBinding = true)]
    public ActionResult BackordersList(GridCommand command, SearchModel model)
    {
        var searchName = model.SearchName;
        var gridData = _service.GetData(searchName);

        model.GridData = new GridModel<Data>
        {
            Data = gridData.Select(PrepareModelForGrid),
            Total = gridData.TotalCount
        };

        return new JsonResult
        {
            Data = model.GridData
        };
    }
于 2013-03-19T18:25:09.470 回答