ViewBag dynamics in ASP. NET MVC3

Source: Internet
Author: User

There is a new dynamic ViewBag feature in MVC 3, which is mainly used to transmit values from the Controller to the view, similar to the ViewData [] dictionary class in use. This is so powerful for ViewBag that you can dynamically set/get values and add any number of additional fields without the need for forced detection.

To see these differences (and when viewbag is not used), let's take a look at the example:

Eample: -- using ViewBag

Controller

 

View plaincopy to clipboardprint?
  1. Public ActionResult Index ()
  2. {
  3. List <string> colors = new List <string> ();
  4. Colors. Add ("red ");
  5. Colors. Add ("green ");
  6. Colors. Add ("blue ");
  7. ViewData ["listColors"] = colors;
  8. ViewData ["dateNow"] = DateTime. Now;
  9. ViewData ["name"] = "Hajan ";
  10. ViewData ["age"] = 25;
  11. Return View ();
  12. }

Public ActionResult Index () </p> <p >{</p> <p> List <string> colors = new List <string> (); </p> <p> colors. add ("red"); </p> <p> colors. add ("green"); </p> <p> colors. add ("blue"); </p> <p> ViewData ["listColors"] = colors; </p> <p> ViewData ["dateNow"] = DateTime. now; </p> <p> ViewData ["name"] = "Hajan"; </p> <p> ViewData ["age"] = 25; </p> <p> return View (); </p> <p >}< br/>

View (ASPX View Engine)

View plaincopy to clipboardprint?
  1. P>
  2. 02 My name is
  3. 03<B> <%: ViewData ["name"] %> </B>,
  4. 04<B> <%: ViewData ["age"] %> </B>Years old.
  5. 05<Br />
  6. 06 I like the following colors:
  7. 07</P>
  8. 08<UlId = "colors">
  9. 09<% Foreach (var color in ViewData ["listColors"] as List<String>) {%>
  10. 10<Li>
  11. 11<FontColor = "<%: color %>"> <%: Color %> </Font>
  12. 12</Li>
  13. 13<%} %>
  14. 14</Ul>
  15. 15<P>
  16. 16<%: ViewData ["dateNow"] %>
  17. 17</P>

P> </p> <p> 02 My name is </p> <p> 03 <B> <%: ViewData ["name"] %> </B>, </p> <p> 04 <B> <%: ViewData ["age"] %> </B> years old. </p> <p> 05 <br/> </p> <p> 06 I like the following colors: </p> <p> 07 </p> <p> 08 <ul id = "colors"> </p> <p> 09 <% foreach (var color in ViewData ["listColors"] as List <string>) {%> </p> <p> 10 <li> </p> <p> 11 <font color = "<%: color %>" ><%: color %> </font> </p> <p> 12 </li> </p> <p> 13 <% }%> </p> <p> 14 </ul> </p> <p> 15 <p> </p> <p> 16 <%: viewData ["dateNow"] %> </p> <p> 17 </p> <br/>

Eample:-Using ViewBag

Controller

View plaincopy to clipboardprint?
  1. Public ActionResult Index ()
  2. 02 {
  3. 03 List <string> colors = new List <string> ();
  4. 04 colors. Add ("red ");
  5. 05 colors. Add ("green ");
  6. 06 colors. Add ("blue ");
  7. 07
  8. 08 ViewBag. ListColors = colors; // colors is List
  9. 09 ViewBag. DateNow = DateTime. Now;
  10. 10 ViewBag. Name = "Hajan ";
  11. 11 ViewBag. Age = 25;
  12. 12 return View ();
  13. 13}

Public ActionResult Index () </p> <p> 02 {</p> <p> 03 List <string> colors = new List <string> (); </p> <p> 04 colors. add ("red"); </p> <p> 05 colors. add ("green"); </p> <p> 06 colors. add ("blue"); </p> <p> 07 </p> <p> 08 ViewBag. listColors = colors; // colors is List </p> <p> 09 ViewBag. dateNow = DateTime. now; </p> <p> 10 ViewBag. name = "Hajan"; </p> <p> 11 ViewBag. age = 25; </p> <p> 12 return View (); </p> <p> 13} <br/>

Are you different from the above comparison?

View (ASPX View Engine)

View plaincopy to clipboardprint?
  1. <P>
  2. My name is
  3. <B> <%: ViewBag. Name %> </B>,
  4. <B> <%: ViewBag. Age %> </B>Years old.
  5. <Br />
  6. I like the following colors:
  7.  </P>
  8.  <UlId = "colors">
  9. <% Foreach (var color in ViewBag. ListColors) {%>
  10. <Li>
  11. <FontColor = "<%: color %>"> <%: Color %> </Font>
  12. </Li>
  13.  <%} %>
  14. </Ul>
  15.  <P>
  16. <%: ViewBag. DateNow %>
  17.  </P>

<P> </p> <p> My name is </p> <B> <%: ViewBag. name %> </B>, </p> <B> <%: ViewBag. age %> </B> years old. </p> <br/> </p> <p> I like the following colors: </p> <ul id = "colors"> </p> <% foreach (var color in viewBag. listColors) {%> </p> <li> </p> <font color = "<%: color %>" ><%: color %> </font> </p> </li> </p> <% }%> </p> </ul> </p> <%: viewBag. dateNow %> </p> <br/>

 

In the preceding example, ViewBag is of the dynamic type. Therefore, we do not need to convert ViewBag. ListColors to a form such as List <string>. On the other hand, this ViewData ["key"] is a peer.

If you use ViewData ["ListColors"] = Colors in your Controller, you can retrieve them through ViewBag. ListColors in View attempts.

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.