简体   繁体   English

来自ASP.NET MVC3中的Form的ReturnUrl

[英]ReturnUrl from Form in ASP.NET MVC3

I have done some research and found that I can use: 我进行了一些研究,发现可以使用:

string retUrl = "";
    if (ViewContext.HttpContext.Request.UrlReferrer != null)
    {
        retUrl =
          ViewContext.HttpContext.Request.UrlReferrer.PathAndQuery;
    }

As a way to set a returnUrl and then pass it into a controller via action link. 作为设置returnUrl并将其通过操作链接传递到控制器的一种方式。

However, is there any way I can pass a parameter from a form into a controller? 但是,有什么方法可以将参数从表单传递到控制器中?

Here's how my code looks right now: 这是我的代码现在的外观:

@using (Html.BeginForm(new { returnUrl = retUrl})) {
    @Html.EditorForModel()
    <input type="submit" value="Save"/>
}

This works great in the sense that it returns you to the right URL when you submit the form. 在您提交表单时,它可以使您返回正确的URL,因此它的工作原理非常好。 However, the form doesn't actually get saved. 但是,该表格实际上并没有保存。 If I remove that returnUrl parameter it saves the form but it does not redirect properly. 如果删除该returnUrl参数,它将保存表单,但无法正确重定向。

This reason I'm doing this is because this form is accessible from multiple pages and I don't want to send them all to one page after they submit the form but rather to the previous page. 我这样做的原因是因为可以从多个页面访问此表单,并且我不希望在提交表单后将它们全部发送到一页,而是发送到上一页。

EDIT 编辑

I've also tried BeginRouteForm and specifying a controller and action, both approaches did not work. 我还尝试了BeginRouteForm并指定了控制器和操作,两种方法均无效。

EDIT 编辑

Action source: 动作来源:

[Authorize]
        [HttpPost]
        public ActionResult EditReview(Review review, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                reviewRepository.SaveReview(review);
                return RedirectToAction("Index");
            }
            return View(review);
        }

To save you form before redirecting you need to switch some lines around in the action method. 要在重定向之前保存表单,您需要在action方法中切换一些行。 Instead of: 代替:

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
     && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
  return Redirect(returnUrl);
}
reviewRepository.SaveReview(review);
return RedirectToAction("Index");

to

reviewRepository.SaveReview(review);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
     && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
  return Redirect(returnUrl);
}
return RedirectToAction("Index");

Like this? 像这样?

@using(Html.BeginForm("act","contr",FormMethod.Post) {
            @Html.Hidden("returnUrl",ViewContext.HttpContext.Request.Url.PathAndQuery)
            <input type="submit" />
        }

There shouldn't be any issue with passing the same in the BeginForm method -- either will bind to ActionMethod(string returnUrl) . 在BeginForm方法中传递相同内容应该没有任何问题-两者都将绑定到ActionMethod(string returnUrl)

If I am understanding you correctly, you are actually changing the ACTION method on the form, so I am not surprised it didn't save. 如果我正确地理解了您,那么您实际上是在更改表单上的ACTION方法,因此并不奇怪它没有保存。 I'd get rid of that and just post to the controller/action as you originally intended. 我会摆脱它,然后按照您的原意将其发布到控制器/操作中。 Then add the retUrl as a hidden input to post along with the rest of the form data. 然后将retUrl添加为隐藏输入,以与其余表单数据一起发布。

@using (Html.BeginForm()) {
    @Html.EditorForModel()
    <input type="hidden" name="returnUrl" value="@retUrl" /> 
    <input type="submit" value="Save"/>
    } 

Edit: 编辑:

You can also use the Html helper. 您还可以使用HTML帮助器。

@Html.Hidden("returnUrl", retUrl)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM