个人感觉 不管它们是否基于session 在用法上最大的区别在于 ViewData用于action方法与前台页面传值使用,TempData除了有viewData的功能外也可以在action方法之间传递值(在本controller里可以传递 跨contoller也是可以的)也可以在重新定向后赋予定向方法的前台页面。现在我是这么理解的 不对了以后再改吧 哈哈!

下面是复制http://news.cnblogs.com/n/69952/的解释

TempData和ViewData的区别

  做个简单的测试看下看下TempData和ViewData的区别:

public ActionResult Test1()
{

TempData[
"text"] = "1-2-3";
ViewData[
"text"] = "1-2-3";
return RedirectToAction("Test2");
}
public ActionResult Test2()
{
string text1 = TempData["text"] as string;
string text2 = ViewData["text"] as string;
return View();
}

  看下面截图,发现经过Test1经过RedirectToAction跳转Test2后,ViewData的值已经被清空,而TempData没有被清空,这是它们的区别之一,我们可以用TempData在Controller之间传递数据。

Asp.net Mvc ViewData和TempData的区别

相关文章:

  • 2022-02-15
  • 2021-11-22
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
猜你喜欢
  • 2022-12-23
  • 2021-10-21
  • 2021-05-24
  • 2022-02-07
  • 2021-05-18
  • 2021-08-01
  • 2021-06-02
相关资源
相似解决方案