grid.mvc在Controller中使用过滤结果
我正在使用grid.mvc( http://gridmvc.codeplex.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"; } _PersonsGrid
@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" })) { }
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”页面上的会话,如下所示:
@helper RenderGridBody() { if (!Model.ItemsToDisplay.Any()) { @Model.EmptyGridText } else { Session["Items"]=Model.ItemsToDisplay; foreach (object item in Model.ItemsToDisplay) { @foreach (IGridColumn column in Model.Columns) { @column.CellRenderer.Render(column, column.GetCell(item)) } } } }
当Grid.MVC填充数据时,选择将保存到会话中,以后可以在执行操作时在控制器中使用该会话。
在控制器中,您只需调用并将变量强制转换为正确的类型:
public ActionResult MyController() { var SelectedRows = (List)Session["Items"]; List listStats = SelectedRows; // the rest of the controller code }
我希望这会有所帮助:)
- 如何使用InvariantCulture仅渲染某些特定的模型字段,而其余的则继续使用用户文化?
- 加密URL中的路由数据
- 在尝试标记odata时,可查询属性未在mvc 4 web api中进行编译
- 来自1 List 的MVC Multiple DropDownLists
- MongoDB引用的最佳实践
- Autofac DbContext已被处理
- 并发检查ICollection(在添加期间)
- 使用Simple Injector注册IAuthenticationManager
- ExecuteCore()vs OnActionExecuting(ActionExecutingContext filterContext)?