我在MVC4 Web應用程序中使用Web網格。我在頁面中有一個搜索功能。網格工作正常,即排序和分頁工作正常,直到沒有搜索執行。執行搜索時,對網格進行排序不會僅對這些搜索結果進行排序,而是會對整個項目列表進行排序。MVC4 Web網格分類和分頁問題
我調試並發現,在點擊Web網格頭進行排序時,它重定向到HttpGet方法,而不是HttpPost.I非常確定,如果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>
)
)
)
}
當用戶搜索一些演員的名字,搜索結果是否正確發生。一旦搜索結束,當用戶點擊網頁標題時,搜索結果不會被正確保留,但控件會再次轉到HttpGET方法而不是HTTPPOST方法。這是主要問題。
指導我如何解決這個問題
請做一些更多的調試,並嘗試將問題縮小到一個代碼片段,然後您可以共享它並得到有用的反饋,既不知道Get方法也不知道我們知道如何應用程序的Post方法看起來像... –
嗨,約翰,我在這裏添加控制器和查看代碼。 –
可能您正在使用ajax功能,使您可以一次加載整個列表的結果,以避免多次調用服務器,這就存在這個問題,當您執行搜索並對'GET' ActionResult進行排序時會再次被調用沒有考慮到搜索過濾器...你可以發佈一個鏈接到webGrid你使用的必須有文檔中的東西 –