met*_*ord 4 jquery webgrid asp.net-mvc-3
基本上,我正在使用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>
Run Code Online (Sandbox Code Playgroud)
我的部分观点:
@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");
}
Run Code Online (Sandbox Code Playgroud)
最后在我的控制器上出现了错误的地方:
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);
}
Run Code Online (Sandbox Code Playgroud)
我还使用一类的WebGrid:作为的WebGrid这里看到: http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667
因此,这实际上适用于过滤,但是一旦获得过滤结果,然后尝试更改缩小搜索结果的排序顺序,就会丢失元素和'name'参数的值,因为WebGrid会再次执行第一个控制器操作.可能这不是最好的方法,但正如我所说,我从未使用过WebGrid,所以我愿意学习.任何帮助将非常感激.谢谢.
小智 5
尝试将FormMethod.Post添加到您的表单,因此请求将转到第二个ActionResult.否则,请求是GET,并且在没有参数的情况下转到第一个ActionResult Index().
| 归档时间: |
|
| 查看次数: |
13224 次 |
| 最近记录: |