一、區別與聯系
ViewData 和 TempData 都可以傳遞弱類型數據,區別如下:TempData 只在當前 Action 中有效,生命周期和 View 相同;保存在Session中,Controller每次執行請求的時候,會從Session中先獲取TempData,而后清除Session,獲取完TempData數據,雖然保存在內部字典對象中,但是其集合中的每個條目訪問一次后就從字典表中刪除。因此TempData 的數據至多只能經過一次Controller傳遞,並且每個元素至多只能被訪問一次,訪問以后,自動被刪除。
ViewData與ViewBag使用的是同一個數據源,因此數據一樣,只是ViewBag 不再是字典的鍵值對結構,而是 dynamic 動態類型(http://www.cnblogs.com/kissdodog/archive/2013/01/20/2868644.html),它會在程序運行的時候動態解析。但是只有當訪問的關鍵字是一個有效的C#標識符時,ViewBag才起作用。例如ViewData["have space"]用ViewBag是無法訪問的。ViewData[]傳遞的key/value,value是Object數據類型,傳遞到View層之后要用as運算轉換一下才能夠變為強類型數據,但是ViewBag是在運行時才確定數據類型,因此根本不用轉換。此處的不用轉換類型是所有的類型,例如能夠隨意傳遞DateTime,String,其他自定義類型等等。但是就效率而言,ViewData[]稍高。
例如:
public ActionResult Index() { ViewData["Name"] = "李四"; ViewBag.Name = "張三"; return View(); }
@ViewData["Name"]; //輸出張三
@ViewBag.Name; //輸出張三
二、使用ViewData或ViewBag傳遞強類型數據
Controller代碼:
public ActionResult Index() { var PeopleList = new List<People_Model>(); for (int i = 0; i < 10; i++) { People_Model p = new People_Model(); p.Id = i; p.Name = "我是" + i; p.Age = 20 + i; PeopleList.Add(p); } ViewData["list"] = PeopleList; return View(); }
public class People_Model { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } }
<table> <tr> <td>編號</td> <td>姓名</td> <td>年齡</td> </tr> @foreach(People_Model item in (ViewData["list"] as IEnumerable<People_Model>)) { <tr> <td>@item.Id</td> <td>@item.Name</td> <td>@item.Age</td> </tr> } </table>
以上代碼再來改寫一下,以說明ViewData與ViewBag的區別,ViewBag是個好東西。先放入一個公共類代碼:
public class Person { public Person(int id, int age, string name) { this.Id = id; this.Age = age; this.Name = name; } public int Id { get; set; } public int Age { get; set; } public string Name { get; set; } }
控制器代碼: 由於ViewBag與ViewData使用的是同一個數據源,因此控制器就使用ViewData傳遞數據,但是在視圖里用不同的東西獲取。
public ActionResult Index() { Person p1 = new Person(1, 20, "張飛"); Person p2 = new Person(2, 21, "關羽"); Person p3 = new Person(3, 22, "劉備"); List<Person> PersonList = new List<Person>(); PersonList.Add(p1); PersonList.Add(p2); PersonList.Add(p3); ViewData["PList"] = PersonList; return View(); }
視圖代碼:
@foreach (var p in ViewBag.PList) //應該注意到此行代碼,根本不用轉換數據類型 { <div style="background-color:red">@p.Name;</div> } @foreach (var p in ViewData["PList"] as List<MVC_AjaxTest.Controllers.Person>) //而用ViewData讀取的話要用as運算符轉換為強類型數據 { <div style="background-color:red">@p.Name;</div> }
三、TempData使用
下面用TempData來實現一個當提交表單時彈出提示,非表單提交不彈出提示的示例:
Controller:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { public class HomeController : Controller { [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string name) { TempData["Tips"] = "表單提交成功!"; return View(); } } }
視圖:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Index</title> <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { <% if(TempData["Tips"] != null) %> <% { %> alert('<%= TempData["Tips"]%>'); <% } %> }) </script> </head> <body> <div> <form action="/Home/Index" method="post"> <input type="text" name="name" value="測試js" /> <input type="submit" /> </form> </div> </body> </html>
不知道大家是否有遇到過,提交了表單之后,老是不知道如何獲取提交成功的提示。實際上,一個普通的表單提交,還真的比較適合頁面提交。如果你出於其他原因覺得不適合使用AJAX的情況下,使用TempData的方式適合於不需要跳轉的情況,提交后直接可以在本頁面繼續操作的情況。
TempData內部是將數據保存在Session里面的,至於實現了哪些接口,不說,因為知道了也沒辦法實現自定義的TempData。
