1

我在 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 方法。这是主要问题。

指导我如何解决这个问题

4

1 回答 1

1

作为一种解决方法,您可以在执行搜索时将网格的状态保存在服务器上,以便您可以在再次渲染网格时检查它,这里回答了类似的问题https://stackoverflow.com/a/ 15528219/335105

于 2013-11-04T07:49:30.580 回答