一、ViewData和ViewBag

1、ViewData和ViewBag都是从Action向View传递数据的方式,当然还有其他方式如向View传递Model。

2、ViewData页面查询数据时需要转换合适的类型(var std in ViewData["students"] as IList<Student>),ViewBag不需要,他们的使用方法如下:

  1)ViewBag:

    controller中定义ViewBag.name="Tom";

    view中@ViewBag.name

  2)ViewData:

    controller中定义ViewBag["name"]="Tom";

    view中如下   

<ul>
@foreach (var std in ViewData["students"] as IList<Student>)
{
<li>
@std.StudentName
</li>
}
</ul>

3、ViewData和ViewBag都是ControllerBase类的属性,ViewData类型是ViewDataDictionary(字典键值对结构),ViewBag类型是dynamic,如下图:

4、ViewData和ViewBag本质一样,在ViewData使用的key不能和ViewBag的key相同,否而回报运行时错误,

http://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc原句:Internally, ViewBag is a wrapper around ViewData. It will throw a runtime exception, if the ViewBag property name matches with the key of ViewData.

http://www.tutorialsteacher.com/mvc/viewdata-in-asp.net-mvc原句:ViewData and ViewBag both use the same dictionary internally. So you cannot have ViewData Key matches with the property name of ViewBag, otherwise it will throw a runtime exception.

5、ViewBag和ViewData仅针对当前Action中有效,生命周期和view相同。

二、TempData


1、TempData类型是TempDataDictionary。

2、 TempData用于把数据从一个action方法传到另一个action方法,两个action可以不再同一个controller中如下代码:

public class HomeController : Controller
{
public ActionResult Index()
{
TempData["name"] = "Tom";
TempData["age"] = ;
return View();
} public ActionResult About()
{
string userName;
int userAge;
if(TempData.ContainsKey("name"]))
userName = TempData["name"].ToString(); if(TempData.ContainsKey("age"]))
userAge = int.Parse(TempData["age"].ToString()); return View();
}
}

3、TempData 在第二次请求后会被清空,第三次请求则获取不到,如下:

第一次请求http://localhost/Home/Index,设置TempData["name"]="Tom";

第二次请求http://localhost/Home/About,可以获取var name = TempData["name"];

第三次请求http://localhost/Home/Details,通过var name = TempData["name"];是获取不到值的。

可以在第二次请求中TempData.Keep();语句来记住TempData["name"],这样第三次就可以继续获取了。

4、TempData 是使用Session存储数据的。

参考文章:http://www.tutorialsteacher.com/mvc/asp.net-mvc-tutorials

asp.net mvc中ViewData、ViewBag和TempData的详解的更多相关文章

  1. 几句话说明 .NET MVC中ViewData, ViewBag和TempData的区别

    ViewData和TempData是字典类型,赋值方式用字典方式, ViewData["myName"] ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.my ...

  2. ASP.NET MVC中viewData、viewBag和templateData的使用与区别

    一:类型比较 1.1)ViewBag是动态类型(dynamic). 1.2)ViewData是一个字典型的(Dictionary)-->ViewDataDictionary. 1.3)TempD ...

  3. ASP.NET MVC中viewData、viewBag和templateData的区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag是动态类型(dynamic),ViewData是一个 ...

  4. Asp.Net Core 中IdentityServer4 实战之角色授权详解

    一.前言 前几篇文章分享了IdentityServer4密码模式的基本授权及自定义授权等方式,最近由于改造一个网关服务,用到了IdentityServer4的授权,改造过程中发现比较适合基于Role角 ...

  5. ASP.NET MVC中ViewData、ViewBag和TempData

    1.ViewData 1.1 ViewData继承了IDictionary<string, object>,因此在设置ViewData属性时,传入key必须要字符串型别,value可以是任 ...

  6. ASP.NET MVC教程六:两个配置文件详解

    前言 在新建完一个MVC项目之后,你会发现整个整个项目结构中存在有两个web.config文件,如下图所示: 这两个配置文件,一个位于项目的根目录下面,一个位于Views文件夹下面,这两个配置文件有什 ...

  7. Asp.net MVC中的ViewData与ViewBag

    Asp.net MVC中的ViewData与ViewBag 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它 ...

  8. ASP.NET MVC中的两个Action之间值的传递--TempData

    一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...

  9. [转]ASP.NET MVC中的两个Action之间值的传递--TempData

    本文转自:ASP.NET MVC中的两个Action之间值的传递--TempData 一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在 ...

随机推荐

  1. Windows phone 8.1布局控件

    布局控件(4种  第一种) Grid:相当于 HTML 中的 Table 标签,但是注意 Table 更重要的是展示数据,   而 Grid 则是专门为布局所生 属性标记: Grid.RowDefin ...

  2. datepicker使用

    JqueryUI作为一个优秀的前端库,被广泛的应用在项目中.之前做的一个排班考勤系统,跟时间打交道较多,对时间控件做过一些对比,觉得jqueryUI里的这个datepicker更为实用,配置起来也简单 ...

  3. 【Javascript】解决Ajax轮询造成的线程阻塞问题(过渡方案)

    一.背景 开发Web平台时,经常会需要定时向服务器轮询获取数据状态,并且通常不仅只开一个轮询,而是根据业务需要会产生数个轮询.这种情况下,性能低下的Ajax长轮询已经不能满足需求,频繁的访问还会造成线 ...

  4. call()与apply()

    1.obj1.method1.call(obj2,argument1,argument2) call的作用就是把obj1的方法放到obj2上使用 2. add 来替换 sub,add.call(sub ...

  5. Tomcat配置HTTPS方式(单向)

    简要记录主要步骤备忘 1.进入到jdk下的bin目录 2.输入如下指令 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat ...

  6. CSS3 justify 文本两端对齐

    浏览器参照基准:Firefox4 and Later, Chrome5 and Later, Safari5 and Later, Opera10.53 and Later, IE5.5 and La ...

  7. [NOIP2016]换教室 D1 T3 Floyed+期望DP

    [NOIP2016]换教室 D1 T3 Description 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 ...

  8. linux系统网址

    主站:http://www.xitongzhijia.net/linux/ linux:http://www.xitongzhijia.net/linux/201603/69275.html (7.2 ...

  9. 移动端/H5关于cursor:pointer导致的问题

    cursor属性规定要显示的光标的类型(形状),该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状(不过 CSS2.1 没有定义由哪个边界确定这个范围). 不过,这个属性用在PC端没有任何问题 ...

  10. Xamarin的不归路-生成安卓错误

    编译生成安卓时提示错误 解决方案:删掉此文件夹(C:\Users\***\AppData\Local\Xamarin\)内所以文件夹和文件,再FQ重新编译即可. 2016年9月1日 13:33