作者: 出处:
ViewData | ViewBag |
它是Key/Value字典集合 | 它是类型对像 |
从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对应UsingViewData View的cshtml:stackholder = new Dictionary (); 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 modules = new List (); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewData["modules"] = modules; return View();}
然后是ViewBag:@ViewData["Title"]
Project Name : @ViewData["ProjectName"]
ProjectDescription :"@ViewData["ProjectDescription"]".
Stack Holder :@foreach ( var stakerholder in ViewData["stackholder"] as Dictionary
) { - @stakerholder.Key : @stakerholder.Value
}Project Details:module List :Project StartDate : @ViewData["StartDate"] Project TotalPrice: @ViewData["TotalPrice"] Project TotaDays : @ViewData["TotalDays"]@foreach ( var module in ViewData["modules"] as List
) { - @module
}
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对应View UsingViewBag 的cshtml的ViewBag:stackholder = new Dictionary (); 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 modules = new List (); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewBag.modules = modules; return View();}
后面是在Controller中使用ViewBag:@ViewBag.Title
Project Name : @ViewBag.ProjectName
ProjectDescription :"@ViewBag.ProjectDescription.
Stack Holder :@foreach ( var stakerholder in ViewBag.stackholder ) {
- @stakerholder.Key : @stakerholder.Value
}Project Details:module List :Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") Project TotalPrice: @ViewBag.TotalPrice ₹ Project TotaDays : @ViewBag.TotalDays@foreach ( var module in ViewBag.modules ) {
- @module
}
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在UsingViewBagInControlAndViewDataInView.cshtml中使用ViewData来取值:stackholder = new Dictionary (); 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 modules = new List (); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewBag.modules = modules; return View();}
反过来是这样的:@ViewData["Title"]
Project Name : @ViewData["ProjectName"]
ProjectDescription :"@ViewData["ProjectDescription"]".
Stack Holder :@foreach ( var stakerholder in ViewData["stackholder"] as Dictionary
) { - @stakerholder.Key : @stakerholder.Value
}Project Details:module List :Project StartDate : @ViewData["StartDate"] Project TotalPrice: @ViewData["TotalPrice"] Project TotaDays : @ViewData["TotalDays"]@foreach ( var module in ViewData["modules"] as List
) { - @module
}
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在UsingViewDataInControlAndViewBagInView.cshtml 的View中使用ViewBag来取值:stackholder = new Dictionary (); 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 modules = new List (); modules.Add("Admin module"); modules.Add("ShoppingCart module"); modules.Add("CMS module"); ViewData["modules"] = modules; return View();}
这样对比看上去会比较清楚一些。在源代码中的ViewBag是这样的属性:@ViewBag.Title
Project Name : @ViewBag.ProjectName
ProjectDescription :"@ViewBag.ProjectDescription.
Stack Holder :@foreach ( var stakerholder in ViewBag.stackholder ) {
- @stakerholder.Key : @stakerholder.Value
}Project Details:module List :Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") Project TotalPrice: @ViewBag.TotalPrice ₹ Project TotaDays : @ViewBag.TotalDays@foreach ( var module in ViewBag.modules ) {
- @module
}
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); }}