【问题标题】:Difference Between ViewData and TempData?ViewData 和 TempData 之间的区别?
【发布时间】:2010-09-15 10:45:19
【问题描述】:

我知道 ViewData 是什么,并且一直在使用它,但是在 ASP.NET Preview 5 中,他们引入了一种新的东西,称为 TempData。

我通常强烈键入我的 ViewData,而不是使用对象字典方法。

那么,我什么时候应该使用 TempData 而不是 ViewData?

对此有什么最佳做法吗?

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

一句话:TempData 和 ViewData 有一个区别:它们只包含两个连续请求之间的数据,之后它们被销毁。您可以使用TempData 传递错误消息或类似的东西。

虽然已经过时,但this article 很好地描述了TempData 的生命周期。

正如 Ben Scheirman 所说的here

TempData 是一个会话支持的临时存储字典,可用于单个请求。在控制器之间传递消息很棒。

【讨论】:

  • 我认为这是一个很大的不同,因为TempData 将涉及一些可能比从方法传递到方法的简单字典更复杂的东西(会话)
【解决方案2】:

当操作返回 RedirectToAction 结果时,它会导致 HTTP 重定向(相当于 Response.Redirect)。在单个 HTTP 重定向请求期间,数据可以保存在控制器的 TempData 属性(字典)中。

【讨论】:

  • 在相同的情况下会保留ViewData的值吗?
  • @Ismail:不,ViewData 不会通过重定向保留数据。这是 TempData 的主要区别。
【解决方案3】:

查看数据:

  • ViewData 是字典类型 public ViewDataDictionary ViewData { get; set; }
  • 它可用于将数据从控制器传递到视图,只有一种方式
  • 它的生命只存在于当前请求中
  • 如果传递字符串则无需类型转换
  • 如果传递对象,那么您需要对其进行类型转换,但在此之前您需要检查它是否不为空
  • 它是ControllerBase 上的一个属性,它是Controller 类的父类

临时数据:

  1. TempData内部使用TempDataDictionary:public TempDataDictionary TempData { get; set; }
  2. 将数据保存到TempDataDictionary 对象后:
    • 它一直存在,可以从任何视图或任何控制器中的任何操作中读取
    • 只能读取一次;一读即为空
    • 它被保存到会话中,因此会话数据过期会丢失。

此行为是 ASP.NET MVC 2 及更高版本的新行为。 在早期版本的 ASP.NET MVC 中,TempData 中的值仅在下一个请求之前可用。

  1. 它是活动的,直到它被读取或会话过期并且可以从任何地方读取。

See the comparison of ViewData, ViewBag, TempData and Session in MVC in detail

【讨论】:

    【解决方案4】:

    我发现这个比较很有用:http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html

    我遇到的一个问题是 TempData 值在默认情况下被读取后被清除。有选项,see methods 'Peek' and 'Keep' on Msdn for more info

    【讨论】:

      【解决方案5】:

      当我们想要将数据从控制器传递到相应的视图时使用视图数据。 视图数据的寿命很短,这意味着它会在重定向发生时销毁。 示例(控制器):

      public ViewResult try1()
          {
              ViewData["DateTime"] = DateTime.Now;
              ViewData["Name"] = "Mehta Hitanshi";
              ViewData["Twitter"] = "@hitanshi";
              ViewData["City"] = "surat";
              return View();
          }
      

      try1.cshtm

      <table>
      <tr>
          <th>Name</th>
          <th>Twitter</th>
          <th>Email</th>
          <th>City</th>
          <th>Mobile</th>
      </tr>
      <tr>
          <td>@ViewData["Name"]</td>
          <td>@ViewData["Twitter"]</td>
          <td>@ViewData["City"]</td>
      </tr>
      </table> 
      

      TempData 在控制器之间或动作之间传输数据。 它用于存储一次性消息,它的寿命很短。我们可以使用 TempData.Keep() 使其在所有操作中都可用或使其持久化。

      示例(控制器):

      public ActionResult try3()
          {
              TempData["DateTime"] = DateTime.Now;
              TempData["Name"] = "Ravina";
              TempData["Twitter"] = "@silentRavina";
              TempData["Email"] = "Ravina12@gmail.com";
              TempData["City"] = "India";
              TempData["MobNo"] = 9998975436;
              return RedirectToAction("TempView1");
          }
          public ActionResult TempView1()
          {
              return View();
          }
      

      TempView1.cshtm

      <table>
      <tr>
          <th>Name</th>
          <th>Twitter</th>
          <th>Email</th>
          <th>City</th>
          <th>Mobile</th>
      </tr>
      <tr>
          <td>@TempData["Name"]</td>
          <td>@TempData["Twitter"]</td>
          <td>@TempData["Email"]</td>
          <td>@TempData["City"]</td>
          <td>@TempData["MobNo"]</td>
      </tr>
      </table>
      

      【讨论】:

        【解决方案6】:

        只是对 TempData 的补充说明。
        其中的数据在下一个请求之前不会存储,而是在下一个读取操作被调用之前存储!
        见:
        TempData won't destroy after second request

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-21
          • 1970-01-01
          • 2011-06-09
          • 1970-01-01
          • 2019-02-03
          • 2021-12-25
          • 2020-05-10
          • 2014-09-20
          相关资源
          最近更新 更多