简体   繁体   English

在MVC4中,如何使用Url中的参数从控制器动作重定向到视图?

[英]In MVC4, how to redirect to a view from a controller action with parameters in the Url?

In my MVC4 login view, I have several sections defined: 在我的MVC4登录视图中,我定义了几个部分:

  • login - /User/Login#login or - /User/Login 登录-/ User / Login#login或-/ User / Login
  • register - /User/Login#register 注册-/ User / Login#register
  • lostpassword - - /User/Login#lostpassword 密码丢失--/ User / Login#lostpassword

The JS on the page picks up the right form area to present. 页面上的JS选择了正确的表单区域来显示。

Everything works fine for login - validation, etc. However if the registration fails, how do I return to the view with the added #register parameter? 一切都能正常进行登录-验证等。但是,如果注册失败,如何使用添加的#register参数返回视图?

-- Update -- I forgot to mention that this is from a controller and all validation errors need to be passed back to it. -更新-我忘了提到这是来自控制器的,所有验证错误都需要传回给它。 So, return Redirect("...") doesn't work 因此, return Redirect("...")不起作用

The following does the trick 以下是技巧

return Redirect(Url.Action("Login", "User") + "#registerform");

URL is ~/Login/User#registerform . URL是~/Login/User#registerform

This way will work: 这种方式将起作用:

return RedirectToAction("NewAction", new { 
     id = model.Id, 
     registerParam = model.registerId
});

PS: If New Action fails you can do the same thing back to the current action method, with some error parameter. PS:如果“新操作”失败,则可以使用某些错误参数将相同操作返回到当前操作方法。

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

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