在 ASP.Net-MVC 中的控制器之间传递信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2269376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Information Between Controllers in ASP.Net-MVC
提问by Jacob Bellamy
This is a duplicate of How to RedirectToAction in ASP.NET MVC without losing request data
这是如何在 ASP.NET MVC 中 RedirectToAction 而不会丢失请求数据的副本
Hi, I have come into a problem which is making me scratch my head a little bit. Basically I have a login page Login.aspx , which has username and password fields, as well as an important little checkbox. The login is handled in the AccountController Login method. The code currently is as follows:
嗨,我遇到了一个问题,这让我有点摸不着头脑。基本上我有一个登录页面 Login.aspx ,它有用户名和密码字段,以及一个重要的小复选框。登录是在 AccountController Login 方法中处理的。目前的代码如下:
[AcceptVerbs(HttpVerbs.Post)]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification =
"Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, string returnUrl,
bool sendStoredInfo)
{
if (!this.ValidateLogOn(userName, password)) {
return View();
}
this.FormsAuth.SignIn(userName, false);
if (!String.IsNullOrEmpty(returnUrl)) {
return Redirect(returnUrl);
} else {
return RedirectToAction("Index", "Home");
}
}
Basically, if the line return Redirect(returnUrl); fires, then it will end up in another controller, the OpenIDController, and it is that situation where the sendStoredInfo bool becomes important. But the problem is I have no reference to it when I'm in the OpenIDController. How can I send this value across?
基本上,如果该行返回 Redirect(returnUrl); 触发,然后它将在另一个控制器 OpenIDController 中结束,在这种情况下,sendStoredInfo bool 变得很重要。但问题是当我在 OpenIDController 中时我没有参考它。如何发送此值?
回答by Philip Wardlaw
Also consider using TempData to pass data from controller to controller. This can be advantageous as you wont have to expose the bool sendFlaginterface potentially to the user.
还可以考虑使用 TempData 将数据从控制器传递到控制器。这可能是有利的,因为您不必将bool sendFlag界面潜在地暴露给用户。
Code in the first controller:
第一个控制器中的代码:
TempData["sendFlag"] = sendStoredInfo;
return RedirectToAction("LoginFailed");
Code in the second controller:
第二个控制器中的代码:
public ActionResult LoginFailed()
{
bool sendFlag = TempData.ContainsKey("sendFlag")? TempData["sendFlag"]: false;
}
回答by Neil T.
Change the call to:
将调用更改为:
return RedirectToAction("LoginFailed", new { sendFlag = sendStoredInfo });
The controller action method signature could be something like:
控制器操作方法签名可能类似于:
public ActionResult LoginFailed(bool sendFlag)
{
...
}
回答by casperOne
Because of the nature of redirects, you can only perform a GET operation.
由于重定向的性质,您只能执行 GET 操作。
This means that you have to pass the parameter as part of the query string.
这意味着您必须将参数作为查询字符串的一部分传递。
So you would redirect to a url like http://host/dir/page?sendStoredInfo=true
所以你会重定向到像http://host/dir/page?sendStoredInfo=true这样的 url
Then, you can chose to have it part of your method signature in the other controller, or, you can choose to access it directly using the HttpRequest exposed by the HttpContext for the operation.
然后,您可以选择将它作为其他控制器中方法签名的一部分,或者,您可以选择直接使用 HttpContext 为操作公开的 HttpRequest 访问它。
You can also call the RedirectToAction, as per this previous question:
您还可以根据上一个问题调用 RedirectToAction:
How to RedirectToAction in ASP.NET MVC without losing request data
回答by Alix
As far as my knowledge serves me well, four different methods exist to handle passing data between controllers in asp.net MVC. They are 1. ViewData 2. ViewBag 3. TempData and 4. Sessions. If you may like a relatively good explanation besides a downloadable sample, please take a look at here
据我所知,有四种不同的方法可以处理在 asp.net MVC 中的控制器之间传递数据。它们是 1. ViewData 2. ViewBag 3. TempData 和 4. Sessions。除了可下载的示例之外,如果您还喜欢比较好的解释,请看这里

