Asp.net MVC ReturnUrl variable not showing up with

2019-08-10 11: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" }))
{
    ...
}