本文导读:在asp.net mvc程序设计中,传递数据常常会用到viewdata、viewbag。ViewData是一个字典集合,通过key值读取对应的value;ViewBag是动态类型,作用和ViewData一样,都是用来存储信息,有一些不同的是ViewData是object类型,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(, , );
ViewData["TotalPrice"] = ;
ViewData["TotalDays"] = ;
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>
<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 &nbsp; : @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>

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(, , );
ViewBag.TotalPrice = ;
ViewBag.TotalDays = ;
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>
<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 &nbsp; : @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>
05-11 15:18