grid.mvc使用过滤使得控制器 [英] grid.mvc use filtered Result in Controller

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

问题描述

使用grid.mvc我'(的http://gridmvc.$c$cplex.com/ )用于筛选和排序。是否有人知道如何来处理动作控制器的过滤效果。我试图通过的FormCollection传递一个隐藏字段,但分页原因只有看得见的值传递。或者有没有在任何MVC很好的选择方格,您可以过滤和排序,并在使用筛选的结果,一个动作和MVCController?

_customersGrid.cshtml

  @using GridMvc.Html
@using GridMvc.Site.Models
@using GridMvc.Sorting
@model GridMvc.Site.Models.Grids.CustomersGrid@ {
    ViewBag.Title =_CustomersGrid;
}< H2> _PersonsGrid< / H>
@ Html.Grid(型号).Named(customersGrid)列(列=>
    {        columns.Add(O => o.CustomerID)
            .EN codeD(假)
        .Sanitized(假)
        .SetWidth(30)
             .RenderValueAs(O => Html.Hidden(客户ID,o.CustomerID));        columns.Add(O => o.CompanyName)
                .Titled(姓名)
                .SetWidth(110);        columns.Add(O => o.Phone)
               .Titled(手机)
               .SetWidth(250);
    })。WithPaging(15).Sortable()。可筛选()。WithMultipleFilters()

Index.cshtml

  @ {
    ViewBag.Title =家;
}    @using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{@class =形横}))
{<&字段集GT;
<传奇>< /传说>        @ Html.Action(网格)@ *电网的局部视图* @          &所述p为H.;
             @ Html.ActionLink(返回,索引,空,新的{@class =BTN,@ ACCESSKEY =B})
       <按钮式=提交级=BTN BTN-主要ACCESSKEY =S>< U> S< / U> AVE< /按钮>
        &所述; / P>
    < /字段集>
}

HomeController的操作

 公众的ActionResult指数()
        {
            返回查看();
        }
        [HttpPost]
        公众的ActionResult指数(的FormCollection形式)
        {
            VAR filterSettings =会话[网过滤器]作为IGridFilterSettings;
            VAR URL =新UriBuilder(Url.Action(NULL,NULL,NULL,Request.Url.Scheme));
            如果(filterSettings!= NULL)
                url.Query = GetGridFilterQueryString(filterSettings); //恢复电网的过滤器设置
            / *如何从的FormCollection获得电网insteat过滤的值* /
           VAR chckedValues​​ = form.GetValues​​(客户编号);            的foreach(在chckedValues​​ VAR ID)
            {
                //做一点事
                的Debug.WriteLine(ID);
            };
            ViewBag.ActiveMenuTitle =演示;
            返回重定向(url.ToString());
        }        公众的ActionResult网格()
        {
            变种库=新CustomersRepository();
            变种网格=新CustomersGrid(repository.GetAll());            会话[网过滤器] = grid.Settings.FilterSettings;在会话//存储网格过滤器
            返回PartialView(_ CustomersGrid网格);        }


解决方案

我终于找到了只发送过滤后的结果来控制的方式。
解决的办法是选择保存到会话中的共享/ _Grid.cshtml页这样的:

  @helper RenderGridBody()
{
如果(!Model.ItemsToDisplay.Any())
{
< TR类=网空文>
    < TD合并单元格=@ Model.Columns.Count()>
        @ Model.EmptyGridText    < / TD>
< / TR>
}
其他
{
    会话[项目] = Model.ItemsToDisplay;
    的foreach(在Model.ItemsToDisplay对象项)
    {
< TR类=格列@ Model.GetRowCssClasses(项目)>
    @foreach(在Model.Columns IGridColumn列)
    {
        @ column.CellRenderer.Render(列,column.GetCell(项目))
    }
< / TR>
    }
}
}

当Grid.MVC用数据填充,选择被保存到稍后可以在控制器执行一个动作时,可以使用会话。

在控制器中,你只需要调用和转换的变量为正确的类型:

 公众的ActionResult myController的()
    {
        VAR SelectedRows =(列表< ModelType>)会议[项目];        清单< ModelType> listStats = SelectedRows;        //控制器code其余
}

我希望会有所帮助:)

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 Actions

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

        }

解决方案

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

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天全站免登陆