The use of data transfer viewbag in MVC

Source: Internet
Author: User

Original link: http://blog.csdn.net/fanbin168/article/details/44803437

ViewBag

The use and difference of ViewBag, ViewData and TempData in MVC3

At the beginning of the MVC3, the view data can be accessed through the ViewBag property, and ViewData is used in the MVC2. The use of ViewData is retained in the MVC3. ViewBag is dynamic, and ViewData is a typical character (Dictionary).

The difference between ViewBag and ViewData:
ViewBag is no longer a key-value-pair structure of a dictionary, but dynamically dynamic type, which is dynamically parsed when the program is running. So no type conversion is required to get its data in the view

ViewData ViewBag
It's a collection of Key/value dictionaries. It is dynamic type pair like
It's from ASP.net MVC 1. asp.net MVC3 only
Based on ASP.net 3.5 framework Based on ASP.net 4.0 and the. NET Framework
ViewData faster than ViewBag. ViewBag is slower than ViewData.
You need to convert the appropriate type when querying data in ViewPage Type conversion is not required when querying data in ViewPage
There are some type conversion codes Better readability
Contorller [CSharp]  View plain  copy <pre class= "CSharp"  name= "code" >using NewOjbect.Models;    using system;   using system.collections.generic;   using System.Linq;    using system.web;   using system.web.mvc;      namespace  NewOjbect.Controllers   {       public class  homecontroller : controller       {                  public actionresult index ()            {                ViewBag.UserName =  "Salt-free Sea";                ViewBag.Age =  ";  "              viewbag.gender = 1;                   string[] Itmes = new string[] {  "China",   "America",  "Germany"  };                viewbag.itemsa = itmes;// viewbag is a new dynamic type keyword wrapper  //viewdata["Items"] =  items;                              return view ();                              }       }  }  
View

[HTML] view plain copy <pre class= "html" name= "code" >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.