asp.net-mvc 在 ASP.NET MVC 中使用 Tempdata - 最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12422930/
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
Using Tempdata in ASP.NET MVC - Best practice
提问by Yasser Shaikh
I am using ASP.NET MVC 3 to build a web application.
我正在使用 ASP.NET MVC 3 来构建 Web 应用程序。
What I am trying to do is pass values between two controllers, though there are many ways to do this I am particular interested in using TempDatafor this.
我想做的是在两个控制器之间传递值,尽管有很多方法可以做到这一点,但我对此特别感兴趣TempData。
public ActionResult Action1()
{
string someMessage;
Test obj = SomeOperation();
if(obj.Valid)
{
someMessage = obj.UserName;
}
else
{
someMessage = obj.ModeratorName;
}
TempData["message"] = someMessage;
return RedirectToAction("Index");
}
public ActionResult Index()
{
ViewBag.Message = TempData["message"]
return View();
}
So is the use of TempDatahere correct ? I mean under best programming practices is this correct way of using TempData?
那么TempData这里的使用正确吗?我的意思是在最佳编程实践下,这是正确的使用方式TempData吗?
In what real time cases should TempDatabe used ?
在什么实时情况下应该TempData使用?
Note :I have gone through the following links
注意:我已经浏览了以下链接
- When to use TempData vs Session in ASP.Net MVC
- http://www.gregshackles.com/2010/07/asp-net-mvc-do-you-know-where-your-tempdata-is/
- 何时在 ASP.Net MVC 中使用 TempData 与 Session
- http://www.gregshackles.com/2010/07/asp-net-mvc-do-you-know-where-your-tempdata-is/
Thanks
谢谢
回答by Display Name
TempDatais a bucket where you can dump data that is only needed for the following request. That is, anything you put into TempData is discarded after the next request completes. This is useful for one-time messages, such as form validation errors. The important thing to take note of here is that this applies to the next request in the session, so that request can potentially happen in a different browser window or tab.
TempData是一个存储桶,您可以在其中转储仅用于以下请求的数据。也就是说,在下一个请求完成后,您放入 TempData 的任何内容都将被丢弃。这对于一次性消息很有用,例如表单验证错误。这里要注意的重要一点是,这适用于会话中的下一个请求,因此该请求可能会发生在不同的浏览器窗口或选项卡中。
To answer your specific question: there's no right way to use it. It's all up to usability and convenience. If it works, makes sense and others are understanding it relatively easy, it's good. In your particular case, the passing of a parameter this way is fine, but it's strange that you need to do that (code smell?). I'd rather keep a value like this in resources (if it's a resource) or in the database (if it's a persistent value). From your usage, it seems like a resource, since you're using it for the page title.
回答您的具体问题:没有正确的使用方法。这完全取决于可用性和便利性。如果它有效,有意义并且其他人相对容易理解它,那就太好了。在您的特定情况下,以这种方式传递参数很好,但是您需要这样做很奇怪(代码味道?)。我宁愿在资源(如果它是资源)或数据库(如果它是持久值)中保留这样的值。从您的使用情况来看,它似乎是一种资源,因为您将它用于页面标题。
Hope this helps.
希望这可以帮助。
回答by Yogi
Please note that MVC 3 onwards the persistence behavior of TempData has changed, now the value in TempData is persisted until it is read, and not just for the next request.
请注意,MVC 3 以后 TempData 的持久性行为已经改变,现在 TempData 中的值将持久化直到它被读取,而不仅仅是下一个请求。
The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request. https://msdn.microsoft.com/en-in/library/dd394711%28v=vs.100%29.aspx
TempData 的值一直存在,直到它被读取或直到会话超时。以这种方式持久化 TempData 可以实现重定向等场景,因为 TempData 中的值在单个请求之外可用。 https://msdn.microsoft.com/en-in/library/dd394711%28v=vs.100%29.aspx
回答by Mojtaba
Just be aware of TempData persistence, it's a bit tricky. For example if you even simply read TempData inside the current request, it would be removed and consequently you don't have it for the next request. Instead, you can use Peekmethod. I would recommend reading this cool article:
请注意 TempData 持久性,这有点棘手。例如,如果您甚至只是在当前请求中读取 TempData,它将被删除,因此您在下一个请求中没有它。相反,您可以使用Peek方法。我建议阅读这篇很酷的文章:

![asp.net-mvc MVC [HttpPost/HttpGet] 用于操作](/res/img/loading.gif)