一、ViewBag和ViewData的定义
public dynamic ViewBag { get; }
public ViewDataDictionary ViewData { get; set; }
二、ViewBag和ViewData的差别
viewdata | viewbag |
它是key/value字典集合 | 它是dynamic类型对象 |
从asp.net mvc1就有了 | 从asp.netmvc3才有 |
基于asp.netframework 3.5 | 基于asp.net framework4.0 |
viewdata比viewbag快 | viewbag比viewdata慢 |
页面查询数据时须要转换合适的类型 | 在页面查询数据时不须要转换合适的类型 |
有一些类型转换代码 | 可读性较好 |
ViewBag不再是字典的键值对结构。而是dynamic动态类型,它会在程序执行的时候动态解析。
ViewBag.NumberObjs = new string[] { "one", "two", "three" };
ViewData["NumberArrs"] = new string[] { "one", "two", "three" };
//正确,dynamic类型的解析作用,foreach能够使用
foreach (var item in ViewBag.NumberObjs)
{
}
//错误:foreach语句不可作用于object类型对象上
foreach (var item in ViewData["NumberArrs"])
{
}
1、ViewBag事实上本质就是ViewData,仅仅是多了层Dynamic控制。所以,使用何种方式全然取决于你的个人爱好。
2、ViewBag和ViewData仅针对当前Action中有效,生命周期和view同样。
三、TempData
保存在session中,Controller每次运行请求时。会从session中获取tempdata。而后清空session,获取完tempdata数据,尽管保存在内部字典中。
可是字典中每一个条目訪问一次后就从字典中删除。因此Tempdata数据最多仅仅能经过一次controller传递。而且每一个元素最多仅仅能訪问一次,之后删除。
四、举例
<pre name="code" class="csharp">public ActionResult UsingViewData()
{
ViewData["Title"] = "Using ViewData";
ViewData["ProjectName"] = "My Test Project";
ViewData["ProjectDescription"] = "This is TestProject to demo Viewdata and viewbag details";
ViewData["StartDate"] = new DateTime(2011, 1, 1);
ViewData["TotalPrice"] = 1000;
ViewData["TotalDays"] = 100;
Dictionary<string, string> stackholder = new Dictionary<string, string>();
stackholder.Add("Client", "Mr.Client");
stackholder.Add("Manager","Mr.Joy");
stackholder.Add("Team Leader","Mr.Toy");
stackholder.Add("Sr.developer","Mr.dojoy");
stackholder.Add("developer","Mr.nodoy");
ViewData["stackholder"]=stackholder;
List<string>modules =new List<string>();
modules.Add("Admin module");
modules.Add("ShoppingCart module");
modules.Add("CMS module");
ViewData["modules"]=modules;
return View();
}
<div><span style="color: rgb(35, 35, 35); font-family: Verdana, Arial, helvetica, sans-seriff; font-size: 14px; line-height: 25px; orphans: 2; text-align: -webkit-auto; widows: 2;">相应UsingViewData View的cshtml:</span></div><pre name="code" class="html">@{
ViewBag.Title = "UsingViewData";
}
<h1>@ViewData["Title"]</h1>
<div>
<div>
<h2>Project Name : @ViewData["ProjectName"]</h2>
</div>
<div>
ProjectDescription :
<p>"@ViewData["ProjectDescription"]".</p>
</div>
<div>
Stack Holder :
<br />
<ul id="stakholder">
@foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
{
<li>
@stakerholder.Key : @stakerholder.Value
</li>
}
</ul>
</div>
<div>
Project Details:<br />
<div>
module List :
<ul id="modulelist">
@foreach ( var module in ViewData["modules"] as List<string> )
{
<li>
@module
</li>
}
</ul>
</div>
Project StartDate : @ViewData["StartDate"] <br />
Project TotalPrice: @ViewData["TotalPrice"] <br />
Project TotaDays : @ViewData["TotalDays"]
</div>
</div>
ViewBag controller:
public ActionResult UsingViewBag()
{
ViewBag.Title = "Using ViewBag";
ViewBag.ProjectName = "My Test Project";
ViewBag.ProjectDescription = "This is Test Project to demo Viewdata and viewbag details";
ViewBag.StartDate = new DateTime(2011, 1, 1);
ViewBag.TotalPrice = 1000;
ViewBag.TotalDays = 100;
Dictionary<string, string> stackholder = new Dictionary<string, string>();
stackholder.Add("Client", "Mr.Client");
stackholder.Add("Manager", "Mr.Joy");
stackholder.Add("Team Leader", "Mr.Toy");
stackholder.Add("Sr.developer", "Mr.doyjoy");
stackholder.Add("developer", "Mr.nodoy");
ViewBag.stackholder = stackholder;
List<string> modules = new List<string>();
modules.Add("Admin module");
modules.Add("ShoppingCart module");
modules.Add("CMS module");
ViewBag.modules = modules;
return View();
}
相应的页面:
@{
ViewBag.Title = "UsingViewBag";
}
<h1>@ViewBag.Title</h1>
<div>
<div>
<h2>Project Name : @ViewBag.ProjectName</h2>
</div>
<div>
ProjectDescription :
<p>"@ViewBag.ProjectDescription.</p>
</div>
<div>
Stack Holder :
<br />
<ul id="stakholder">
@foreach ( var stakerholder in ViewBag.stackholder )
{
<li>
@stakerholder.Key : @stakerholder.Value
</li>
}
</ul>
</div>
<div>
Project Details:<br />
<div>
module List :
<ul id="modulelist">
@foreach ( var module in ViewBag.modules )
{
<li>
@module
</li>
}
</ul>
</div>
Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
Project TotalPrice: @ViewBag.TotalPrice ₹ <br />
Project TotaDays : @ViewBag.TotalDays
</div>
</div>
五、总结
1、ViewData和TempData是字典类型,赋值方式用字典方式。ViewData["myName"]
2、ViewBag是动态类型,使用时直接加入属性赋值就可以 ViewBag.myName
3、ViewBag和ViewData仅仅在当前Action中有效,等同于View
4、TempData能够通过转向继续使用,由于它的值保存在Session中。但TempData仅仅能经过一次传递。之后会被系统自己主动清除
5、ViewData和ViewBag中的值能够互相訪问。由于ViewBag的实现中包括了ViewData