1

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

Rakesh Kumar
  • 55
  • 1
  • 8
  • please do some more debugging and try to narrow down the problem to a code snippet probably which you can then share it on SO and get useful feedback, neither we have idea of Get method nor we know how the Post method of your application looks like... – John x Oct 31 '13 at 07:19
  • Hi John, I am adding the controller and View Code here. – Rakesh Kumar Nov 04 '13 at 07:24
  • probably you are using the ajax functionality which enables you to load the entire list of results at once so that to avoid multiple calls to the server, there lies the problem, when you perform the search and sort the `GET` ActionResult is called again without taking into consideration the search filters... can you post a link to webGrid you are using there must be something in the docs – John x Nov 04 '13 at 07:43
  • here is a link to similar question (unanswered)http://stackoverflow.com/questions/18703974/webgrid-ajax-how-to-make-it-work-with-mvc-4-w-razor – John x Nov 04 '13 at 07:45

1 Answers1

1

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

Community
  • 1
  • 1
John x
  • 4,031
  • 8
  • 42
  • 67