asp.net mvc中ViewData、ViewBag和TempData的详解
一、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的详解的更多相关文章
- 几句话说明 .NET MVC中ViewData, ViewBag和TempData的区别
ViewData和TempData是字典类型,赋值方式用字典方式, ViewData["myName"] ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.my ...
- ASP.NET MVC中viewData、viewBag和templateData的使用与区别
一:类型比较 1.1)ViewBag是动态类型(dynamic). 1.2)ViewData是一个字典型的(Dictionary)-->ViewDataDictionary. 1.3)TempD ...
- ASP.NET MVC中viewData、viewBag和templateData的区别
在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag是动态类型(dynamic),ViewData是一个 ...
- Asp.Net Core 中IdentityServer4 实战之角色授权详解
一.前言 前几篇文章分享了IdentityServer4密码模式的基本授权及自定义授权等方式,最近由于改造一个网关服务,用到了IdentityServer4的授权,改造过程中发现比较适合基于Role角 ...
- ASP.NET MVC中ViewData、ViewBag和TempData
1.ViewData 1.1 ViewData继承了IDictionary<string, object>,因此在设置ViewData属性时,传入key必须要字符串型别,value可以是任 ...
- ASP.NET MVC教程六:两个配置文件详解
前言 在新建完一个MVC项目之后,你会发现整个整个项目结构中存在有两个web.config文件,如下图所示: 这两个配置文件,一个位于项目的根目录下面,一个位于Views文件夹下面,这两个配置文件有什 ...
- Asp.net MVC中的ViewData与ViewBag
Asp.net MVC中的ViewData与ViewBag 在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它 ...
- ASP.NET MVC中的两个Action之间值的传递--TempData
一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
- [转]ASP.NET MVC中的两个Action之间值的传递--TempData
本文转自:ASP.NET MVC中的两个Action之间值的传递--TempData 一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在 ...
随机推荐
- 在hive中遇到的错误
1:如果在将文件导入到hive表时,查询结果为null(下图) 这个是因为在创建表的时候没有指定列分隔符,hive的默认分隔符是ctrl+a(/u0001) 2.当我复制好几行sql到hive命令 ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- oj Rapid Typing
import bs4 import requests import urllib2 import time import base64 session=requests.Session() respo ...
- 同一台机子上用多个git 账号
Step 1 - Create a New SSH Key We need to generate a unique SSH key for our second GitHub account. 1 ...
- PKUSC2016
day x(x<0) 外出培训倒数第二天晚上发烧了....逃过了第二天早上的考试,orz 抢到rank 1 的commonc神犇!! day 0 下午到了北大,发了两张50元饭卡.这是第三次来北 ...
- 搭建centos测试环境:window安装xshell,WinSCP 。 centos安装jdk tomcat
通过ssh实现远程访问linux系统: 由于xshell 连接centos,需要centos开启ssh服务.所以先启动SSH服务,没有ssh需要先安装. 1 . 查看SSH是否安装命令:rpm -qa ...
- linux设备驱动
http://blog.csdn.net/bob_fly1984/article/details/8820670 struct ov5640_data { struct ov5640_platf ...
- 真刀真枪压测:基于TCPCopy的仿真压测方案
郑昀 基于刘勤红和石雍志的实践报告 创建于2015/8/13 最后更新于2015/8/19 关键词:压测.TCPCopy.仿真测试.实时拷贝流量 本文档适用人员:技术人员 提纲: 为什么要做仿真测试 ...
- ZeroMQ接口函数之 :zmq_bind - 绑定一个socket
ZeroMQ 官方地址 : http://api.zeromq.org/4-0:zmq-bind zmq_bind(3) ZMQ Manual - ZMQ/3.2.5 Name zmq_bind - ...
- OSG的节点访问
OSG的节点访问 转自:http://www.cnblogs.com/kanego/archive/2011/09/27/2193484.html SG中节点的访问使用的是一种访问器模式. 一个典型的 ...