网格.在控制器中使用过滤的结果
本文关键字:过滤 结果 控制器 网格 | 更新日期: 2023-09-27 18:04:32
我使用grid.mvc(http://gridmvc.codeplex.com/)进行过滤和排序。有人知道如何在动作控制器中处理过滤的结果吗。我试图通过FormCollection传递一个隐藏的字段,但由于只传递了可见的值。或者在mvc中有任何好的替代网格,在那里你可以过滤和排序,并使用过滤的结果在和mvc控制器中的动作?
_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>
}
<<p> HomeController行动/strong> 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"上的会话。
@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>
}
}
}
当网格。MVC中填充了数据,选择被保存到会话中,以后在控制器中执行动作时可以使用。
在控制器中,您只需要调用并将变量强制转换为正确的类型:
public ActionResult MyController()
{
var SelectedRows = (List<ModelType>)Session["Items"];
List<ModelType> listStats = SelectedRows;
// the rest of the controller code
}
我希望这对你有帮助。