Asp.net mvc 4 MVC4 Web网格排序和分页问题

Asp.net mvc 4 MVC4 Web网格排序和分页问题,asp.net-mvc-4,Asp.net Mvc 4,我正在MVC4 Web应用程序中使用Web网格。我在页面中有搜索功能。Web网格工作正常,即排序和分页工作正常,直到没有执行搜索为止。在执行搜索时,对web网格进行排序不会单独对搜索结果进行排序,而是对整个项目列表进行排序 我调试后发现,在点击Web网格头进行排序时,它会重定向到HttpGet方法,而不是HttpPost。我非常确定,如果HttpPost被命中,那么这个问题就会消失 我试着在谷歌上搜索,但找不到任何具体的答案。任何帮助或指点都将不胜感激。希望我清楚我的问题 控制器: pu

我正在MVC4 Web应用程序中使用Web网格。我在页面中有搜索功能。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();
            }

        }
public ActionResult Index()
{   
var item=GetAllActors();
返回视图(项目);
}
[HttpPost]
公共操作结果索引(字符串SearchContion、FormCollection集合)
{
var item=GetAllActors();
List listOfItems=新列表();
if(item!=null&&collection!=null)
{
如果(!string.IsNullOrEmpty(SearchContion))
{
List searchResults=item.FindAll(s=>s.ActorName.IndexOf(SearchContion,StringComparison.OrdinalIgnoreCase)>=0);
foreach(搜索结果中的var数据)
{
ActorBE actor=新ActorBE();
actor=item.Where(l=>l.ActorName==data).FirstOrDefault();
添加(参与者);
}  
返回视图(列表项);
}
其他的
{
返回视图(项目);
}
}
其他的
{
返回视图();
}
}
视图:

@model IEnumerable
@{
ViewBag.Title=“Actor”;
Layout=“~/Views/Shared/_Layout.cshtml”;
WebGrid grid=新的WebGrid(rowsPerPage:50,canPage:true,canSort:true);
Pager(WebGridPagerModes.All);
Bind(Model,rowCount:Model.ToList().Count());
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
搜寻
演员名
@Html.TextBox(“SearchContion”)
@GetHtml(htmlAttributes:new
{id=“grid”},
表样式:“webgrid”,
标题样式:“webgrid标题”,
页脚样式:“webgrid页脚”,
交替行样式:“webgrid交替行”,
selectedRowStyle:“webgrid选定行”,
firstText:“第一”,
lastText:“Last”,
下一步:“下一步”,
模式:WebGridPagerModes.All,
上一篇:“上一篇”,
行样式:“webgrid行样式”,列:grid.columns
(
grid.Column(“ActorID”,标题:“参与者ID,样式:“Column”,canSort:true),
grid.Column(“ActorName”,标题:“参与者名称”,样式:“宽度:200px”,canSort:true),
网格.柱
("",
标题:“行动”,
格式:@
@ActionLink(“编辑”,“编辑”,新的{id=item.ActorID})
@如果(项目IsActive)
{
@ActionLink(“停用”、“删除”,新建{id=item.ActorID})
}
)
)
)       
}
当用户搜索某个参与者名称时,搜索结果会正常进行。搜索结束后,当用户单击web网格标题时,搜索结果不会正确保留,但控件会再次转到HttpGET方法,而不是HTTPPOST方法。这是主要问题


指导我如何解决此问题

作为一项工作,您可以在执行搜索时在服务器上保存网格的状态,以便在再次渲染网格时进行检查,这里回答了一个类似的问题

作为一项工作,您可以在执行搜索时在服务器上保存网格的状态因此,您可以在再次渲染网格时检查它,这里回答了一个类似的问题

请进行更多调试,并尝试将问题缩小到一个代码片段,然后您可以将其共享,并获得有用的反馈。我们既不知道get方法,也不知道应用程序的Post方法看起来如何e、 …嗨,John,我在这里添加控制器和视图代码。可能您正在使用ajax功能,它允许您一次加载整个结果列表,以避免多次调用服务器。当您执行搜索和排序时,再次调用GETActionResult而不考虑cons时,问题就出现了考虑一下搜索过滤器……你能发布一个链接到你正在使用的webGrid吗?文档中一定有东西。这是一个类似问题的链接(未回答)请进行更多的调试,并尝试将问题缩小到一个代码段,然后您可能可以将其共享,并获得有用的反馈,我们既不知道get方法,也不知道应用程序的Post方法是什么样子……嗨,John,我在这里添加控制器和视图代码。可能您正在使用ajax功能ty使您能够一次加载整个结果列表,以避免多次调用服务器,问题就出在您执行搜索和排序时,
GET
ActionResult再次被调用,而不考虑搜索过滤器……您是否可以发布一个链接到您正在使用的webGridn docshere是指向类似问题(未回答)的链接。谢谢John。我猜此会话解决方案可以实施。我将尝试此方法并让您知道…谢谢…)谢谢John。我猜此会话解决方案可以实施。我将尝试此方法
@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>
)
)
)       
 }