ViewData与ViewBag的区别
ViewData与ViewBag的区别视图数据可以通过ViewBag属性访问,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类。
一、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);
}
}
三、ViewData与ViewBag的区别
1、ViewData是字典类型,赋值方式用字典方式,通过key值读取对应的value, ViewData["myName"]
2、ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.myName
3、ViewBag和ViewData只在当前Action中有效,等同于View
4、ViewData和ViewBag 中的值可以互相访问,因为ViewBag的实现中包含了ViewData
四、ViewData与ViewBag的实例
1、ViewData
(1)、在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();
}
(2)、对应UsingViewData View的cshtml
<h1>@ViewData["Title"]</h1>
<li>
<li>
<h2>Project Name : @ViewData["ProjectName"]</h2>
</li>
<li>
ProjectDescription :
<p>"@ViewData["ProjectDescription"]".</p>
</li>
<li>
Stack Holder :
<br />
<ul id="stakholder">
@foreach ( var stakerholder in ViewData["stackholder"] as Dictionary<string, string> )
{
<li>
@stakerholder.Key : @stakerholder.Value
</li>
}
</ul>
</li>
<li>
Project Details:<br />
<li>
module List :
<ul id="modulelist">
@foreach ( var module in ViewData["modules"] as List<string> )
{
<li>
@module
</li>
}
</ul>
</li>
Project StartDate : @ViewData["StartDate"] <br />
Project TotalPrice: @ViewData["TotalPrice"] <br />
Project TotaDays : @ViewData["TotalDays"]
</li>
</li>
2、ViewBag
(1)、在Controller中使用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();
}
(2)、对应View UsingViewBag 的cshtml的ViewBag
<h1>@ViewBag.Title</h1>
<li>
<li>
<h2>Project Name : @ViewBag.ProjectName</h2>
</li>
<li>
ProjectDescription :
<p>"@ViewBag.ProjectDescription.</p>
</li>
<li>
Stack Holder :
<br />
<ul id="stakholder">
@foreach ( var stakerholder in ViewBag.stackholder )
{
<li>
@stakerholder.Key : @stakerholder.Value
</li>
}
</ul>
</li>
<li>
Project Details:<br />
<li>
module List :
<ul id="modulelist">
@foreach ( var module in ViewBag.modules )
{
<li>
@module
</li>
}
</ul>
</li>
Project StartDate : @ViewBag.StartDate.ToString("dd-MMM-yyyy") <br />
Project TotalPrice: @ViewBag.TotalPrice ₹ <br />
Project TotaDays : @ViewBag.TotalDays
</li>
</li>