MVC C# 临时数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10487008/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:02:10  来源:igfitidea点击:

MVC C# TempData

c#asp.net-mvctempdata

提问by Nate Pet

Can somebody please explain the purpose of TempData in MVC. I understand it behaves like ViewBag but what does it do beyond that.

有人可以解释一下 MVC 中 TempData 的目的吗?我知道它的行为类似于 ViewBag,但除此之外还有什么作用。

采纳答案by Jakub Konecki

TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this. Therefore, the only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view. Looking back at the previous HomeController code sample means that the TempData object could yield results differently than expected because the next request origin can't be guaranteed. For example, the next request can originate from a completely different machine and browser instance.

TempData 是一个非常短暂的实例,您应该只在当前和后续请求期间使用它!由于 TempData 以这种方式工作,您需要确定下一个请求将是什么,并且重定向到另一个视图是唯一可以保证这一点的时间。因此,使用 TempData 可靠工作的唯一情况是在您重定向时。这是因为重定向会终止当前请求(并将 HTTP 状态代码 302 Object Moved 发送到客户端),然后在服务器上创建一个新请求来为重定向视图提供服务。回顾之前的 HomeController 代码示例意味着 TempData 对象可能产生与预期不同的结果,因为无法保证下一个请求来源。例如,

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

回答by Sampath

ViewBag

查看包

Allows you to create dynamic properties

允许您创建动态属性

Passing data between the controller and view

在控制器和视图之间传递数据

Controller

控制器

ViewBag.Name= "Lion";

ViewBag.Name= "Lion";

View

看法

<div>
  <h4>@ViewBag.Name</h4>
 </div>

TempData

临时数据

TempData is meant to be a very short-lived instance

TempData 是一个非常短暂的实例

you should only use it during the current and the subsequent requests only

您应该只在当前和后续请求中使用它

TempData dictionary is used to share data between controller actions

TempData 字典用于在控制器操作之间共享数据

TempData["Name"] = "Lion";

I have written Blog post about this.Check that How to use Asp.Net MVC TempData Properly ?

我已经写了关于这个的博客文章。检查如何正确使用 Asp.Net MVC TempData ?

回答by itzmebibin

TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

TempData 是一个字典对象,它派生自 TempDataDictionary 类并存储在短期会话中。

public TempDataDictionary TempData { get; set; }

It is a property of ControllerBase class.It is used to pass data from current request to subsequent request (means redirecting from one page to another). It's life is very short and lies only till the target view is fully loaded. It's required typecasting for getting data and check for null values to avoid error.It is used to store only one time messages like error messages, validation messages.

它是 ControllerBase 类的一个属性。它用于将数据从当前请求传递到后续请求(意味着从一个页面重定向到另一个页面)。它的生命周期很短,只有在目标视图完全加载后才有效。获取数据和检查空值以避免错误需要类型转换。它仅用于存储一次性消息,如错误消息、验证消息。