Asp.net MVC中的ViewData与ViewBag
生活随笔
收集整理的這篇文章主要介紹了
Asp.net MVC中的ViewData与ViewBag
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在Asp.net MVC 3 web應用程序中,我們會用到ViewData與ViewBag,對比一下:?
?
| ViewData | ViewBag |
| 它是Key/Value字典集合 | 它是dynamic類型對像 |
| 從Asp.net MVC 1 就有了 | ASP.NET MVC3 才有 |
| 基于Asp.net 3.5 framework | 基于Asp.net 4.0與.net framework |
| ViewData比ViewBag快 | ViewBag比ViewData慢 |
| 在ViewPage中查詢數據時需要轉換合適的類型 | 在ViewPage中查詢數據時不需要類型轉換 |
| 有一些類型轉換代碼 | 可讀性更好 |
?
在Controller中使用ViewData:
public ActionResult UsingViewData() {ViewData["Title"] = " Using ViewData";ViewData["ProjectName"] = "My Test Project";ViewData["ProjectDescription"] = "This is Test Project 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(); }
?
對應UsingViewData View的cshtml:
<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:
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.dojoy");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(); }
?
對應View UsingViewBag 的cshtml的ViewBag:
<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>
?
后面是在Controller中使用ViewBag:
public ActionResult UsingViewBagInControlAndViewDataInView() {ViewBag.Title = " Using ViewBag In Control And ViewData In View";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.dojoy");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(); }
?
在UsingViewBagInControlAndViewDataInView.cshtml中使用ViewData來取值:
<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>
?
反過來是這樣的:?
public ActionResult UsingViewDataInControlAndViewBagInView() {ViewData["Title"] = " Using ViewData In Control And ViewBag In View";ViewData["ProjectName"] = "My Test Project";ViewData["ProjectDescription"] = "This is Test Project 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(); }
?
在UsingViewDataInControlAndViewBagInView.cshtml 的View中使用ViewBag來取值:
<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>
?
這樣對比看上去會比較清楚一些。在源代碼中的ViewBag是這樣的屬性:
public dynamic ViewBag {get {if (_dynamicViewData == null) {_dynamicViewData = new DynamicViewDataDictionary(() => ViewData);}return _dynamicViewData;} }
?
ViewData是:
public ViewDataDictionary ViewData {get {if (_viewData == null) {SetViewData(new ViewDataDictionary());}return _viewData;}set {SetViewData(value);} }
?
轉載于:https://www.cnblogs.com/Liyuting/p/8491793.html
總結
以上是生活随笔為你收集整理的Asp.net MVC中的ViewData与ViewBag的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个改掉坏脾气的个性签名!
- 下一篇: 《感鹤》第十四句是什么