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 TempData
for 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 TempData
here correct ? I mean under best programming practices is this correct way of using TempData
?
那么这里使用TempData是正确的吗?我的意思是,在最佳编程实践中,这种使用TempData的方式是正确的吗?
In what real time cases should TempData
be used ?
在什么情况下应该使用TempData ?
Note : I have gone through the following links
注意:我已经通过了以下链接。
- When to use TempData vs Session in ASP.Net MVC
- 何时在ASP中使用TempData vs会话。Net MVC
- http://www.gregshackles.com/2010/07/asp-net-mvc-do-you-know-where-your-tempdata-is/
- http://www.gregshackles.com/2010/07/asp-net-mvc-do-you-know-where-your-tempdata-is/
Thanks
谢谢
3 个解决方案
#1
61
TempData
is 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是一个bucket,您可以在其中转储仅用于以下请求的数据。也就是说,在下一个请求完成后,您放入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.
希望这个有帮助。
#2
43
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
#3
12
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 Peek
method. I would recommend reading this cool article:
只要注意到TempData持久性,就有点麻烦了。例如,如果您只是简单地在当前请求中读取TempData,那么它将被删除,因此对于下一个请求您没有它。相反,您可以使用Peek方法。我建议你读读这篇很酷的文章:
MVC Tempdata , Peek and Keep confusion
MVC Tempdata, Peek和Keep混淆。
#1
61
TempData
is 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是一个bucket,您可以在其中转储仅用于以下请求的数据。也就是说,在下一个请求完成后,您放入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.
希望这个有帮助。
#2
43
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
#3
12
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 Peek
method. I would recommend reading this cool article:
只要注意到TempData持久性,就有点麻烦了。例如,如果您只是简单地在当前请求中读取TempData,那么它将被删除,因此对于下一个请求您没有它。相反,您可以使用Peek方法。我建议你读读这篇很酷的文章:
MVC Tempdata , Peek and Keep confusion
MVC Tempdata, Peek和Keep混淆。