Reputation: 55
I am using Web Grid in MVC4 web application. I have a search functionality in the page. Web grid works fine i.e Sorting and Paging works fine until there is no search performed. When a search is performed, then sorting the web grid does not sort those searched results alone but sorts the entire list of items.
I debugged and found that during click of Web grid header for sorting, it redirects to HttpGet method and not HttpPost.I am pretty sure that if HTTPPOST is hit, then this problem would vanish.
I tried searching in google but could not find any specific answers. Any help or pointers would be greatly appreciated. Hope I am clear on my problem.
Controller:
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();
}
}
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>
)
)
)
}
When user searches some actor name, the search results are happening properly. Once search is over, when the user clicks on web grid headers, then search results are not retained properly but the control again goes to HttpGET Method and not to the HTTPPOST Method. This s the main problem.
Guide me on how to solve this problem
Upvotes: 1
Views: 7413
Reputation: 4031
As a work around what you can do is when search is performed save the state of the Grid on server so that you can check for it while rendering the grid again, a similar question was answered here https://stackoverflow.com/a/15528219/335105
Upvotes: 1