【发布时间】:2012-03-03 08:35:29
【问题描述】:
基本上,我使用的是 WebGrid,我需要过滤结果。我在这里遇到的第一个问题是这是我第一次使用 WebGrid,我希望你们中的一些人可以帮助我...到目前为止,我已经设法对网格结果进行排序并使用 Ajax 过滤它们,但是,当重新排序过滤后的结果,子集丢失了,我带着全套结果回到开头。我当然知道为什么会这样,但我不知道如何让它发挥作用。
例子:
在我看来:
@model IQueryable<Cities>
@section MoreScripts
{
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
}
@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "GridData"}))
{
<fieldset>
<legend>Search Filters</legend>
<br />
<div>
Name
</div>
<div>
@Html.TextBox("Name")
</div>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
}
<div id="GridData">
@Html.Partial("Grid", Model)
</div>
我的部分观点:
@model IQueryable<Cities>
@{
var grid = new WebGrid<Cities>(null,rowsPerPage: 5, defaultSort: "Nombre", ajaxUpdateContainerId: "GridData");
grid.Bind(Model, autoSortAndPage: true, rowCount: Model.Count());
@grid.GetHtml(columns:
grid.Columns(
grid.Column("Name", "Name", canSort: true),
grid.Column("CreationDate", "Creation Date", canSort: true),
grid.Column("Active", "Active", canSort: true, format: @<text><input type="checkbox" disabled="disabled" value="@item.ID" @(item.Active == true ? "Checked" : null) /></text>),
grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "editLink smallCell", @title = "Edit" })),
grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "deleteLink smallCell", @title = "Delete" }))),
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style");
}
最后在我的控制器上做错了:
public ActionResult Index()
{
return View(repository.GetAllRecords().OrderByDescending(f => f.CreationDate));
}
[HttpPost]
public ActionResult Index(string name)
{
var data = repository.GetAllRecords();
if(!string.IsNullOrEmpty(name))
data = data.Where(a => a.Name.Contains(name));
data = data.OrderByDescending(f => f.CreationDate);
return PartialView("Grid", data);
}
我也在使用一个 WebGrid 类:WebGrid 如下所示: http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667
因此,这实际上适用于过滤,但是一旦您获得过滤结果,然后尝试更改缩小搜索结果的排序顺序,您将丢失元素和“名称”参数的值,因为 WebGrid 再次出现第一个控制器动作。可能这不是最好的方法,但正如我所说,我从未使用过 WebGrid,所以我愿意学习。任何帮助将非常感激。谢谢。
【问题讨论】:
-
我认为这个答案:stackoverflow.com/a/10052663/1651536 解决了你的问题
标签: asp.net-mvc-3 jquery webgrid