Asp.net MVC ReturnUrl variable not showing up with method FormMethod.Post in Html.BeginForm

荒凉一梦 提交于 2019-12-24 06:35:29

问题


The default template in MVC3 sets a 'returnurl' variable in the query string of the logon page. This page then posts back to a controller

@using (Html.BeginForm()) {

That is then picked up in the controller like so

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {

I wanted to add a CSS class to the form so I changed the helper to:

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { @class = "form-horizontal" }))

But now information in the query string isn't getting set in the controller.

I could always set a hidden input value to the retrunurl in the form but I didn't know if there was a simpler way.

Thanks


回答1:


You need to use a hidden field in this case because this overload doesn't preserve the original query string which contained the returnurl variable. Or if you don't want to use a hidden field you could use a query string parameter:

@using (Html.BeginForm(null, null, new { returnUrl = Request["returnurl"] }, FormMethod.Post, new { @class = "form-horizontal" }))
{
    ...
}


来源:https://stackoverflow.com/questions/9144930/asp-net-mvc-returnurl-variable-not-showing-up-with-method-formmethod-post-in-htm

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