1、ViewBag和ViewData的定義html
public dynamic ViewBag { get; } public ViewDataDictionary ViewData { get; set; } public dynamic ViewBag { get; } public ViewDataDictionary ViewData { get; set; }
2、ViewBag和ViewData的區別web
viewdata | viewbag |
它是key/value字典集合 | 它是dynamic類型對象 |
從asp.net mvc1就有了 | 從asp.netmvc3纔有 |
基於asp.netframework 3.5 | 基於asp.net framework4.0 |
viewdata比viewbag快 | viewbag比viewdata慢 |
頁面查詢數據時須要轉換合適的類型 | 在頁面查詢數據時不須要轉換合適的類型 |
有一些類型轉換代碼 | 可讀性較好 |
ViewBag再也不是字典的鍵值對結構,而是dynamic動態類型,它會在程序運行的時候動態解析。session
1 ViewBag.NumberObjs = new string[] { "one", "two", "three" }; 2 ViewData["NumberArrs"] = new string[] { "one", "two", "three" }; 3 4 //正確,dynamic類型的解析做用,foreach可使用 5 foreach (var item in ViewBag.NumberObjs) 6 { 7 8 } 9 //錯誤:foreach語句不可做用於object類型對象上 10 foreach (var item in ViewData["NumberArrs"]) 11 { 12 13 }
一、ViewBag其實本質就是ViewData,只是多了層Dynamic控制。因此,使用何種方式徹底取決於你的我的愛好。mvc
二、ViewBag和ViewData僅針對當前Action中有效,生命週期和view相同。asp.net
3、TempDataspa
保存在session中,Controller每次執行請求時,會從session中獲取tempdata,然後清空session,獲取完tempdata數據,雖然保存在內部字典中。可是字典中每一個條目訪問一次後就從字典中刪除。所以Tempdata數據最多隻能通過一次controller傳遞,而且每一個元素最多隻能訪問一次,以後刪除。.net
4、舉例code
在Controller中使用ViewData:htm
<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>
5、總結
一、ViewData和TempData是字典類型,賦值方式用字典方式,ViewData["myName"]
二、ViewBag是動態類型,使用時直接添加屬性賦值便可 ViewBag.myName
三、ViewBag和ViewData只在當前Action中有效,等同於View
四、TempData能夠經過轉向繼續使用,由於它的值保存在Session中。但TempData只能通過一次傳遞,以後會被系統自動清除
五、ViewData和ViewBag中的值能夠互相訪問,由於ViewBag的實現中包含了ViewData