The use of ViewData and viewbag in asp.net mvc

Source: Internet
Author: User

When doing MVC development, often use viewdata and viewbag to carry out the value operation, because is very commonly used so summarizes here, in order to facilitate the up-and. One, the use method of ViewBag

Background code:

Public ActionResult Index ()
{
    dictionary<string, string> address = new dictionary<string, string> ( );
    Address. ADD ("Lng", "12.353535");
    Address. ADD ("Lat", "28.262626");
    Address. ADD ("Location", "No. Tenth Downing Street");
    list<string> modules = new list<string> ();
    Modules. ADD ("Admin module");
    Modules. ADD ("recursive module");
    Modules. ADD ("consistent module");
    Viewbag.name = "Grasshopper";
    Viewbag.age = "a";
    Viewbag.phone = "18233199999";
    viewbag.address = address;
    Viewbag.modules = Modules;
    return View ();
}

Foreground code:
Background with viewbag value, the foreground can either be viewbag by the value, or through the ViewData value. When you use ViewData, you must convert the data to the appropriate type, and you do not need to convert the data type when you use ViewBag to take a value.

@ViewData ["Name"]
@ViewData ["Age"]
@ViewData ["Phone"]
@{
    dictionary<string, string> dict = viewdata["Address"] as dictionary<string, string>;
}
@if (dict!= null)
{
    @dict ["Lng]"
    @dict ["Lat"]
    @dict ["Location"]
}
@{
    list< string> list = viewdata["Modules"] as list<string>;
}
@if (list!= null)
{
    @list [0]
    @list [1]
    @list [2]    
}
<br/>
@ViewBag. Name
@ViewBag. Age
@ViewBag. Phone
@ViewBag address
@ViewBag. address["Lng"
@ viewbag.address["Lat"]
@ViewBag. address["Location"]
@ViewBag. Modules
@ViewBag. modules[0]
@ VIEWBAG.MODULES[1]
@ViewBag. modules[2]
second, the use method of ViewData

Background code:

Public ActionResult Index ()
{
    dictionary<string, string> address = new dictionary<string, string> ( );
    Address. ADD ("Lng", "12.353535");
    Address. ADD ("Lat", "28.262626");
    Address. ADD ("Location", "No. Tenth Downing Street");
    list<string> modules = new list<string> ();
    Modules. ADD ("Admin module");
    Modules. ADD ("recursive module");
    Modules. ADD ("consistent module");
    viewdata["Name"] = "Grasshopper";
    Viewdata["age"] = "a";
    viewdata["Phone"] = "18233199999";
    viewdata["Address"]=address;
    viewdata["Modules"] = Modules;
    return View ();
}

Foreground code:
Background with viewdata value, the foreground can either be viewbag by the value, or through the ViewData value. When you use ViewData, you must convert the data to the appropriate type, and you do not need to convert the data type when you use ViewBag to take a value.

@ViewData ["Name"]
@ViewData ["Age"]
@ViewData ["Phone"]
@{
    dictionary<string, string> dict = viewdata["Address"] as dictionary<string, string>;
}
@if (dict!= null)
{
    @dict ["Lng]"
    @dict ["Lat"]
    @dict ["Location"]
}
@{
    list< string> list = viewdata["Modules"] as list<string>;
}
@if (list!= null)
{
    @list [0]
    @list [1]
    @list [2]    
}
<br/>
@ViewBag. Name
@ViewBag. Age
@ViewBag. Phone
@ViewBag address
@ViewBag. address["Lng"
@ viewbag.address["Lat"]
@ViewBag. address["Location"]
@ViewBag. Modules
@ViewBag. modules[0]
@ VIEWBAG.MODULES[1]
@ViewBag. modules[2]
iii. definition of both

Definition of ViewBag:

Public dynamic ViewBag {get  
    {  
        if (_dynamicviewdata = = null) {  
            _dynamicviewdata = new Dynamicviewdatadicti Onary (() => ViewData);  
        Return _dynamicviewdata  
    }  
}

Definition of ViewData:

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

By definition, we can see that viewbag is a viewdata dynamic wrapper, which is equivalent to the encapsulation process on the basis of ViewData. Iv. The difference between the two ViewData is the dictionary type, the assignment way uses the dictionary way, reads the corresponding value,viewdata["MyName" by the key value ViewBag is the dynamic type, when uses directly through the attribute assignment value, viewbag.myname ViewData and ViewBag are valid only in the current action, equivalent to view

Values in ViewData and viewbag can be accessed from one another

Attention:
1, ViewBag only works when the keyword is a valid C # identifier.
For example, if you hold a value in viewdata["Key with space", you cannot use viewbag access because it cannot be compiled at all.
2. Dynamic values cannot be passed as a parameter to an extension method, because the C # compiler must 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, then the compilation is not done.
For example: @Html. TextBox ("name", Viewbag.name) will fail to compile.
There are two ways to make this line of code compile: @Html. TextBox ("Name", (string) viewbag.name), @Html. TextBox ("Name", viewdata["name") Five, simple summary

Encounter the unknown things do not worry, now every day there are so many new knowledge available, no one can remember all the points of knowledge, as long as you can calm down to seriously study always learn, we must believe that they can do, do not set limits on their own.

Related Article

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.