MVC foreground gets the values in an array of ViewData

Source: Internet
Author: User

Checked the information for a morning, found a more effective method

Background controller: Publicactionresult Index ()

{

list<string> colors = new list<string> ();

Colors.  ADD ("Red");

Colors.  ADD ("green");

Colors.  ADD ("Blue");

viewdata["listcolors"] = colors;

return View ();

}

Front Desk Interface:

@foreach (var color in viewdata["Listcolors"] as List<string>)

{

@color

}

I think this is relatively clear and simple.

There are several other ways of transmitting values (data transfer between view and action)

ViewBag Dynamic Type

Background controller: Publicactionresult Index ()

{

  dictionary<string, string> stackholder = new dictionary<string, string> ();    Stackholder. ADD ("Client", "Mr.  client");    Stackholder. ADD ("Manager", "Mr. Joy");    Stackholder. ADD ("Team Leader", "Mr.toy");    Stackholder. ADD ("Sr. Developer", "Mr.dojoy");    Stackholder. ADD ("Developer", "Mr Nodoy");    Viewbag.stackholder = Stackholder;

return View ();

}

Front Desk Interface:

 @ Viewbag.stackholder

ViewData Weakly-State

Model Dynamic Type

Backstage: Return View (data)//equivalent to deposit Viewdata.model

Front Desk: Model

MVC foreground gets the values in an array of ViewData

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.