1 Message_ViewData Message_ViewBag"]
The running results are the same. Here they are interconnected.
Differences between ViewBag and ViewData:
Use ViewBag
ViewBag is no longer a dictionary key-Value Pair structure, but a dynamic type. It will be dynamically parsed when the program is running.
Controller code:
1 public ActionResult Index () 2 {3 string [] items = new string [] {"one", "two", "three"}; 4 ViewBag. items = items; // viewbag is a new dynamic keyword package // ViewData ["Items"] = items; 5 return View (); 6}
View code:
1 <ul>2 @foreach (dynamic p in ViewBag.Items)3 { 4 <li>The item is: @p</li>5 }6 </ul>
Dynamic p can be replaced by var p or string p.
Execution result:
Use ViewData
If ViewData is used, the following error occurs:
If we want to use ViewData, We need to manually convert it to an array. Through debugging, we can see that
1 string[] items = new string[] { "one", "two", "three" };2 ViewBag.Items = items;3 ViewData["Items"] = items;
Comparison between the two
The ViewBag and ViewData after the value assignment are both string arrays. For example:
Only ViewData is of the object type, while ViewBag is of the dynamic type.The difference between the dynamic type and the object type is that it will automatically convert according to the data type during use, while the object type needs to be forcibly converted by ourselves.. For example, when ViewBag. Items is traversed above, it is automatically converted according to the data type, while ViewData requires forced conversion, as shown below:
1 @foreach (string a in (string[])ViewData["Items"])2 {3 <li>The item is: @a</li>4 }
In addition, we can see from the definition:
1 [Dynamic]2 public dynamic ViewBag { get; }3 public ViewDataDictionary ViewData { get; set; }
Here, ViewBag only has the get method, and there is no set method, but we assigned a value to ViewBag. Through decompilation, we found that the ViewBag code is as follows:
1 [Dynamic] 2 public object ViewBag 3 { 4 [return: Dynamic] 5 get 6 { 7 Func<ViewDataDictionary> viewDataThunk = null; 8 if (this._dynamicViewDataDictionary == null) 9 {10 if (viewDataThunk == null)11 {12 viewDataThunk = () => this.ViewData;13 }14 this._dynamicViewDataDictionary = new DynamicViewDataDictionary(viewDataThunk);15 }16 return this._dynamicViewDataDictionary;17 }18 }
It is not hard to see that ViewBag Returns _ dynamicViewDataDictionary. Continue to trace and find that _ dynamicViewDataDictionary belongs to the DynamicViewDataDictionary class. Its code is as follows:
1 internal sealed class failed: DynamicObject 2 {3 // Fields 4 private readonly Func <ViewDataDictionary> _ viewDataThunk; 5 6 // Methods 7 public DynamicViewDataDictionary (Func <ViewDataDictionary> viewDataThunk ); 8 public override IEnumerable <string> values (); 9 public override bool TryGetMember (GetMemberBinder binder, out object result); 10 public override bool TrySetMember (SetMemberBinder binder, object value ); 11 12 // Properties13 private ViewDataDictionary ViewData {get;} 14 15 of them include the TryGetMember and TrySetMember methods. Open these two methods: 16 public override bool TrySetMember (SetMemberBinder binder, object value) 17 {18 this. viewData [binder. name] = value; 19 return true; 20} 21 22 public override bool TryGetMember (GetMemberBinder binder, out object result) 23 {24 result = this. viewData [binder. name]; 25 return true; 26} 27}
It is found that ViewBag is actually ViewData, but it only has a layer of Dynamic control.. Therefore, the method you use depends on your personal interests.
Use of TempData
Like ViewData and ViewBag, TempData can also be used to transmit data to views.The only difference is that the life cycle of ViewData and ViewBag is the same as that of View, which is only useful to the current View. TempData can pass values in different actions, similar to the Seesion in webform.. As follows:
1 public ActionResult Index()2 {3 ViewBag.hello = "hello,this is viewBag";4 ViewData["hi"] = "hi,this is viewData";5 TempData["abc"] = "this is tempdata";6 return View();7 }
Call the following in the About View:
1
The page effect is as follows:
Only the value of TempData ["abc"] is obtained here, but the value of TempData is automatically deleted once. Then, I refresh the page, then, TempData ["abc"] is Null.
View the Code through decompilation. It is found that the TempData data is automatically deleted after the call. For more information, see: http://www.cnblogs.com/tristanguo/archive/2009/04/06/1430062.html
(By default, TempData uses sessions to store temporary data. The data stored in TempData is valid only once and is deleted once. This access refers to a request to the next request, because after the next request arrives, the TempData data stored in the Session will be retrieved from the Session and assigned to TempData, then, the data is deleted from the Session. Let's take a look at ASP. net mvc Preview5 source code:
That is to say, TempData is saved only to the next request. After the next request is complete, TempData will be deleted. Note that TempData uses sessions for storage. Sessions correspond to specific users, so there is no concurrency problem.
If you do not want to delete the data in TempData after accessing the next request, you can use the TempData. Keep () method .)
Considerations for other views
<Li> The item is: @ Html. Raw (p) </li> indicates that p is not HTML encoded.
The controller can return this view or other views, as shown below:
1 public ActionResult Index()2 {3 ViewBag.Message_ViewBag = "I am viewbag";4 ViewData["Message_ViewData"] = "I am viewdata";5 return View("About");6 }
When we need to return a view in a completely different directory structure, we can use ~ Symbol to provide the full path of the view to return:
1 return View("~/Views/Home/About.cshtml");
Reference: https://www.cnblogs.com/bianlan/archive/2013/01/11/2857105.html