MVC controller to view values (ViewData and ViewBag)

Source: Internet
Author: User


recently in the development project, the use of the ASP . NET MVC because there is no contact before, for its value of the way some unfamiliar, here to carry out a preliminary summary of the accumulation of learning!

One: Use

ViewData use:

Public ActionResult Index () {list<string> colors = new list<string> (); colors. ADD ("red"); colors. ADD ("green"); colors. ADD ("Blue"); viewdata["listcolors"] = colors; viewdata["Datenow"]= DateTime.Now; viewdata["name"] = "Hejingyuan"; viewdata["Age"]= 25;return View ();

View

<p>my name is <b> @ViewData [' name '] </b>,<b> @ViewData ["Age"] </b>years old.<br/>i Like the following colors:</p><ul id= "Colors" > @foreach (var color in viewdata["Listcolors"] as list< string>) {<li><font color= "@color" > @color </font></li>}</ul><p> @ViewData [" Datenow "]</p>

ViewBag use:

Public ActionResult Index () {list<string>colors = new list<string> (); colors. ADD ("red"); colors. ADD ("green"); colors. ADD ("Blue"); Viewbag.listcolors = colors; Colors is listviewbag.datenow= datetime.now; Viewbag.name= "Hejingyuan"; Viewbag.age = 25;return View (); }

View

<p>my name is <b> @ViewBag. Name</b>, <b> @ViewBag .age</b> years old.<br/>i like the Following Colors:</p><ul id= "Colors" > @foreach (var color in viewbag.listcolors) {<li><font color= "@color" > @color </font> </li>}</ul><p> @ViewBag .datenow</p>

In the use, we can clearly see the difference between the two ways, just access is worth the way some difference!

Two, define

ViewBag definition

Public dynamic ViewBag {    get {        if (_dynamicviewdata = = null) {            _dynamicviewdata = new Dynamicviewdatadictiona Ry (() = ViewData);        }        return _dynamicviewdata;}    }

ViewData definition

Public viewdatadictionary ViewData {    get {        if (_viewdata = = null) {            setviewdata (new Viewdatadictionary ()); c9/>}        return _viewdata;    }    set {        setviewdata (value);    }}

by definition we can see that viewbag is a dynamic wrapper for ViewData, equivalent to ViewData on the basis of the encapsulation process.

Three: Contrast

The difference between ViewData and ViewBag

1, ViewData is a dictionary type, the value of the method is dictionary, through the key value read the corresponding value,viewdata["MyName"]

2, ViewBag is a dynamic type, the use of directly add property assignment can be viewbag.myname

3, ViewBag and ViewData are only valid in the current action, equivalent to the view

4. Values in ViewData and viewbag can be accessed by each other because the ViewBag implementation contains ViewData

Attention:

1 , ViewBag only works if the keyword to be accessed is a valid C # identifier.

For example, if the viewdata["Key with Spaces"] You cannot use a value that is stored in the ViewBag access. Because it doesn't compile at all.

2 , a dynamic value cannot be passed as a parameter to an extension method. Because the C # compiler needs to know the true type of each parameter at compile time in order to choose the correct extension method. If any of these parameters are dynamic, they are not compiled.

For example, this line of code fails to compile: @Html. TextBox ("name", Viewbag.name). There are two ways to make this line of code compile: the first is to use viewdata["Name"], and the second is to convert the Viewbag.name value to a specific type: (String) viewbag.name.

Four: summary

for the value of the topic, for everyone should be some cliché, because as long as the people who contact the project is very understanding, this is a very basic knowledge, listed here, the main purpose is not to tell us how to pass the value, how to operate, and then ask you to write down and so on, But through the above knowledge, tell everyone actually knowledge is not as mysterious as we think, all knowledge even if we have not heard, but as long as we spend some time to study, to summarize, then all the difficulties have become easy !




MVC controller to view values (ViewData and ViewBag)

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.