How do I get my WebGrid to POST instead of GET during a sort or paging operation in my MVC4 site?

江枫思渺然 提交于 2019-12-04 17:45:15

This may not be the most elegant solution, but it works:

Add the model to your Session in the view:

Session.Add( "Model", Model );

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 );

Cleanup your Session accordingly.

Old question, but just to add a reference:

I preferred the solution suggested at this link

which solves the problem using JQuery:

var links = $('a[href*=page], a[href*=sort]'), form = $('form');
links.click(function () {
            form.attr("action", this.href);
            $(this).attr("href","javascript:");
            form.submit();
        });

Further to the JQuery answer above (which brought me success, thanks!) don't forget to undelegate the the webgrid's own magic methods that it adds behind the scenes. Otherwise you may end up with another ajax GET occurring at the same time as your POST.

Before binding to the 'page' and 'sort' links do this:-

    $("#MyWebGridID").undelegate();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!