grid.mvc 在控制器中使用过滤结果 [英] grid.mvc use filtered Result in Controller

查看:13
本文介绍了grid.mvc 在控制器中使用过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 grid.mvc(http://gridmvc.codeplex.com/) 进行过滤和排序.有谁知道如何在动作控制器中处理过滤后的结果.我试图通过 FormCollection 传递一个隐藏字段,但是分页的原因只传递了可见值.或者在 mvc 中是否有任何好的替代网格,您可以在其中过滤和排序并将过滤后的结果用于 MVCController 中的操作?

I'am using grid.mvc(http://gridmvc.codeplex.com/) for filtering and sorting. Does anybody know how to process the filtered result in an action controller. I'm trying to pass a hidden field via FormCollection, but cause of paging only the visible Values are passed. Or is there any good alternative grid in mvc where you can filter and sort and use the filtered result for an action in and MVCController?

_customersGrid.cshtml

    @using GridMvc.Html
@using GridMvc.Site.Models
@using GridMvc.Sorting
@model GridMvc.Site.Models.Grids.CustomersGrid

@{
    ViewBag.Title = "_CustomersGrid";
}

<h2>_PersonsGrid</h2>
@Html.Grid(Model).Named("customersGrid").Columns(columns =>
    {

        columns.Add(o => o.CustomerID)
            .Encoded(false)
        .Sanitized(false)
        .SetWidth(30)
             .RenderValueAs(o => Html.Hidden("CustomerID", o.CustomerID));

        columns.Add(o => o.CompanyName)
                .Titled("Name")
                .SetWidth(110);

        columns.Add(o => o.Phone)
               .Titled("Phone")
               .SetWidth(250);


    }).WithPaging(15).Sortable().Filterable().WithMultipleFilters()

Index.cshtml

@{
    ViewBag.Title = "Home";
}

    @using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form-horizontal" }))
{   

<fieldset>
<legend></legend>

        @Html.Action("Grid") @* grid in a partial view *@

          <p>
             @Html.ActionLink("Back", "Index",null,new { @class = "btn", @accesskey="b" }) 
       <button type="submit"  class="btn btn-primary" accesskey="s" ><u>S</u>ave</button>
        </p>


    </fieldset>
}

HomeController 操作

 public ActionResult Index()
        {


            return View();
        }
        [HttpPost]
        public ActionResult Index(  FormCollection form)
        {
            var filterSettings = Session["grid-filters"] as IGridFilterSettings;
            var url = new UriBuilder(Url.Action(null, null, null, Request.Url.Scheme));
            if (filterSettings != null)
                url.Query = GetGridFilterQueryString(filterSettings); //restore grid filter settings


            /* How to get the filtered values from grid insteat from formcollection*/
           var chckedValues = form.GetValues("CustomerId");

            foreach (var id in chckedValues)
            {
                //Do something
                Debug.WriteLine(id);
            };


            ViewBag.ActiveMenuTitle = "Demo";
            return Redirect(url.ToString());
        }

        public ActionResult Grid()
        {
            var repository = new CustomersRepository();
            var grid = new CustomersGrid(repository.GetAll());



            Session["grid-filters"] = grid.Settings.FilterSettings;//store grid filters in the session
            return PartialView("_CustomersGrid", grid);

        }

推荐答案

我终于找到了只向控制器发送过滤结果的方法.解决方案是将选择保存到Shared/_Grid.cshtml"页面上的会话中,如下所示:

I finally found the way to send only filtered results to controller. The solution is to save the selection to session on the "Shared/_Grid.cshtml" page like this:

@helper RenderGridBody()
{
if (!Model.ItemsToDisplay.Any())
{
<tr class="grid-empty-text">
    <td colspan="@Model.Columns.Count()">
        @Model.EmptyGridText

    </td>
</tr>
}
else
{
    Session["Items"]=Model.ItemsToDisplay;
    foreach (object item in Model.ItemsToDisplay)
    {
<tr class="grid-row @Model.GetRowCssClasses(item)">
    @foreach (IGridColumn column in Model.Columns)
    {
        @column.CellRenderer.Render(column, column.GetCell(item))
    }
</tr>
    }
}
} 

当使用数据填充 Grid.MVC 时,选择将保存到会话中,以便稍后在控制器中执行操作时使用.

When the Grid.MVC is populated with data, the selection is saved to session that can be used later in the controller when executing an action.

在控制器中,您只需调用变量并将其转换为正确的类型:

In the controller, you only have to call and cast the variable into the correct type:

public ActionResult MyController()
    {
        var SelectedRows = (List<ModelType>)Session["Items"];

        List<ModelType> listStats = SelectedRows;

        // the rest of the controller code
}

我希望这会有所帮助:)

I hope that will be helpful :)

这篇关于grid.mvc 在控制器中使用过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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