前面一篇文章我们学习了如何获取View提交给Controller的数据,今天我们接着学习View如何使用ViewData、ViewBag这两种方式获取Controller传递过来的数据。
在开始之前,我们需要了解的是ViewData、ViewBag这两个类都是控制器类的属性,我们在控制器的Action方法中可以直接使用。
1.理论学习
同时,我们发现ViewBag是动态类型,也就是到执行的时候才知道具体的数据。ViewData属性是ViewDataDictionary类型的,查看ViewDataDictionary类定义
看清楚上面代码截图的部分。
(1)ViewDataDictionary类实现了IEnumerable接口,所以可以进行遍历操作
(2)拥有Model属性,可以通过ViewData.Model向前台传递数据
(3)拥有this[key] 属性,意味着我们可以使用索引储存、查询数据
同时需要注意的是,上面的Model、this属性的返回值都是Object类型的,数据到前台之后是需要强制类型转换的。
总结如下所示
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中查询数据时不需要类型转换 |
有一些类型转换代码 | 可读性更好 |
讲解了基础知识,下面我们就结合具体的代码来操作一下。
2.代码测试
1.ViewData 方式
Controller 控制器对应的UsingViewData
public ActionResult UsingViewData()
{
List<string> name = new List<string>();
Dictionary<string,string> grade = new Dictionary<string, string>();
grade.Add("C语言","90");
grade.Add("数据结构", "90");
grade.Add("组成原理", "90");
grade.Add("操作系统", "90");
grade.Add("编译原理", "99");
grade.Add("计算机网络", "95");
grade.Add("网络基础", "92");
ViewData["Grade"] = grade;
ViewData["Username"] = "Bill Gates";
ViewData["UserPassword"] = "110";
ViewData["Ssex"]="1";
ViewData["Address"]="花园路刘庄";
ViewData["Phone"]="119";
return View();
}
对应于UsingViewData的View页面
<html>
<head>
<title>UsingViewData</title>
<style type="text/css">
</head>
<body style="text-align:center">
<div>
<h6>Username:</h6>@ViewData["Username"]<br/>
<h6>UserPassword:</h6>@ViewData["UserPassword"]<br/>
<h6>Ssex:</h6>@ViewData["Ssex"]<br/>
<h6>Address:</h6>@ViewData["Address"]<br/>
<h6>Phone:</h6>@ViewData["Phone"]<br/>
<h6>Grade</h6>
<table style="border:1px solid red">
<tr><th>Phy</th><th>Score</th></tr>
@foreach (var item in ViewData["Grade"] as Dictionary<string, string>)
{
<tr>
<td>@item.Key</td>
<td>@item.Value</td>
</tr>
}
</table>
</div>
</body>
</html>
2.ViewBag方式
Controller 控制器对应的UsingViewBag
public ActionResult UsingViewBag()
{
List<string> name = new List<string>();
Dictionary<string, string> grade = new Dictionary<string, string>();
grade.Add("C语言", "90");
grade.Add("数据结构", "90");
grade.Add("组成原理", "90");
grade.Add("操作系统", "90");
grade.Add("编译原理", "99");
grade.Add("计算机网络", "95");
grade.Add("网络基础", "92");
ViewBag.Grade = grade;
ViewBag.Username = "Bill Gates";
ViewBag.UserPassword = "110";
ViewBag.Ssex = "1";
ViewBag.Address = "花园路刘庄";
ViewBag.Phone = "119";
return View();
}
对应于UsingViewBag的View页面
<html>
<head>
<title>UsingViewBag</title>
</head>
<body>
<div>
<h6>Username:</h6>@ViewBag.Username<br/>
<h6>UserPassword:</h6>@ViewBag.UserPassword<br/>
<h6>Ssex:</h6>@ViewBag.Ssex<br/>
<h6>Address:</h6>@ViewBag.Address<br/>
<h6>Phone:</h6>@ViewBag.Phone<br/>
<h6>Grade</h6>
<table style="border:1px solid red">
<tr><th>Phy</th><th>Score</th></tr>
@foreach (var item in ViewBag.Grade as Dictionary<string, string>)
{
<tr>
<td>@item.Key</td>
<td>@item.Value</td>
</tr>
}
</table>
</div>
</body>
</html>
3.ViewData与ViewBag结合方式
还是使用前面ViewData方式中Controller下面的UsingViewData代码,修改其对应的View页面代码,一样可以达到效果
<html>
<head>
<title>UsingViewData</title>
</head>
<body style="text-align:center">
<div>
<h6>Username:</h6>@ViewBag.Username<br/>
<h6>UserPassword:</h6>@ViewData["UserPassword"]<br/>
<h6>Ssex:</h6>@ViewData["Ssex"]<br/>
<h6>Address:</h6>@ViewData["Address"]<br/>
<h6>Phone:</h6>@ViewData["Phone"]<br/>
<h6>Grade</h6>
<table style="border:1px solid red">
<tr><th>Phy</th><th>Score</th></tr>
@foreach (var item in ViewBag.Grade as Dictionary<string, string>)
{
<tr>
<td>@item.Key</td>
<td>@item.Value</td>
</tr>
}
</table>
</div>
</body>
</html>
上面三种方式中,个人比较喜欢使用第二种。