在MVC4站点中的排序或分页操作期间,如何使WebGrid而不是GET到POST? [英] How do I get my WebGrid to POST instead of GET during a sort or paging operation in my MVC4 site?
问题描述
我有一个相当简单的站点,具有搜索"局部视图和列表"局部视图.使用多种模型将它们汇总到索引"视图中.
I have a fairly simple site with a Search partial view and a Listing partial view. They're rolled up using multiple models into the Index view.
一切都很好.除非我单击网格列标题以排序或尝试分页到下一个数据列表,否则网格将恢复为空.如果我重新提交相同的搜索条件,则网格将重新填充所有正确排序或分页的适用数据.
Everything is fine. Except when I click the grid column headers to sort or attempt to page to the next listing of data, the grid comes back empty. If I re-submit the same search criteria, then the grid repopulates with all applicable data sorted or paged properly.
我已经将此行为归结为WebGrid将其分页和排序机制设置为GET而不是POST的事实.因此,显然我所有的模型数据都没有提交.
I've tracked this behavior down to the fact that the WebGrid sets up it's paging and sorting mechanisms as a GET instead of a POST. So obviously all my model data is left off the submission.
是否没有办法使WebGrid开机自检,所以数据标签也随之而来?对于WebGrid来说,不包含要分页或排序的数据的类似乎适得其反.
Isn't there a way to get the WebGrid to POST so the data tags along? Seems quite counterproductive for the WebGrid as a class to not include the data one wants to page or sort.
推荐答案
这可能不是最优雅的解决方案,但它可以起作用:
This may not be the most elegant solution, but it works:
在视图中将模型添加到您的会话中:
Add the model to your Session in the view:
Session.Add( "Model", Model );
然后,在控制器的Index GET Action(或任何GET Action)中,只需检查该值并调用POST Action:
Then, in the Index GET Action in your controller (or whatever the GET Action is), just check for the value and call the POST Action:
if ( Session[ "Model" ] != null )
this.Index( Session[ "Model" ] as MyModel );
相应地清理您的会话.
这篇关于在MVC4站点中的排序或分页操作期间,如何使WebGrid而不是GET到POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!