【发布时间】:2013-11-04 15:49:30
【问题描述】:
我在 MVC4 Web 应用程序中使用 Web Grid。我在页面中有搜索功能。 Web 网格工作正常,即排序和分页工作正常,直到没有执行搜索。执行搜索时,对 Web 网格进行排序不会单独对这些搜索结果进行排序,而是对整个项目列表进行排序。
我调试并发现在单击Web网格标题进行排序期间,它重定向到HttpGet方法而不是HttpPost。我很确定如果点击HTTPPOST,那么这个问题就会消失。
我尝试在谷歌搜索,但找不到任何具体的答案。任何帮助或指示将不胜感激。希望我清楚我的问题。
控制器:
public ActionResult Index()
{
var item = GetAllActors();
return View(item);
}
[HttpPost]
public ActionResult Index(string SearchContion, FormCollection collection)
{
var item = GetAllActors();
List<ActorBE> listOfItems = new List<ActorBE>();
if (item != null && collection != null)
{
if (!string.IsNullOrEmpty(SearchContion))
{
List<string> searchResults = item.FindAll(s => s.ActorName.IndexOf(SearchContion, StringComparison.OrdinalIgnoreCase) >= 0).Select(p => p. ActorName).ToList();
foreach (var data in searchResults)
{
ActorBE actor = new ActorBE ();
actor = item.Where(l => l.ActorName == data).FirstOrDefault();
listOfItems.Add(actor);
}
return View(listOfItems);
}
else
{
return View(item);
}
}
else
{
return View();
}
}
查看:
@model IEnumerable<Tool.DataService.ActorBE>
@{
ViewBag.Title = "Actor";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(rowsPerPage: 50, canPage: true, canSort: true);
grid.Pager(WebGridPagerModes.All);
grid.Bind(Model, rowCount: Model.ToList().Count());
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div style="padding: 2px 2px 2px 2px;">
<fieldset>
<legend>Search</legend>
<header>
<div class="content-wrapper">
<div class="float-left">
<label style="display:inline;margin-right:5px">Actor Name</label>
@Html.TextBox("SearchContion")
<input type="submit" value="Search" name="Search" style="border-radius:5px;margin-left:5px;"/>
</div>
</div>
</header>
</fieldset>
</div>
@grid.GetHtml(htmlAttributes: new
{ id = "grid" },
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
firstText:"First",
lastText:"Last",
nextText:"Next",
mode: WebGridPagerModes.All,
previousText:"Previous",
rowStyle: "webgrid-row-style", columns: grid.Columns
(
grid.Column("ActorID",header:"Actor ID, style:"column", canSort:true),
grid.Column("ActorName",header:"Actor Name", style:"width:200px", canSort:true),
grid.Column
("",
header:"Actions",
format:@<text>
@Html.ActionLink("Edit", "Edit", new { id = item.ActorID })
@if (item.IsActive)
{
@Html.ActionLink("Deactivate", "Delete", new { id = item. ActorID })
}
</text>
)
)
)
}
当用户搜索某个演员姓名时,搜索结果正常。搜索结束后,当用户单击 Web 网格标题时,搜索结果不会正确保留,但控件再次转到 HttpGET 方法而不是 HTTPPOST 方法。这是主要问题。
指导我如何解决这个问题
【问题讨论】:
-
请进行更多调试并尝试将问题缩小到代码 sn-p,然后您可以在 SO 上分享它并获得有用的反馈,我们既不知道 Get 方法也不知道您的应用程序的 Post 方法看起来如何...
-
嗨约翰,我在这里添加控制器和查看代码。
-
可能您正在使用 ajax 功能,该功能使您能够一次加载整个结果列表,以避免多次调用服务器,当您执行搜索和排序时,问题就出现了
GETActionResult 被再次调用而不考虑搜索过滤器......你可以发布一个链接到你正在使用的 webGrid 文档中必须有一些东西 -
这里是类似问题的链接(未回答)stackoverflow.com/questions/18703974/…
标签: asp.net-mvc-4