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?
- 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"] = "Hajan ";
- ViewData ["age"] = 25;
- Return View ();
- }
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?
- P>
- 02 My name is
- 03<B> <%: ViewData ["name"] %> </B>,
- 04<B> <%: ViewData ["age"] %> </B>Years old.
- 05<Br />
- 06 I like the following colors:
- 07</P>
- 08<UlId = "colors">
- 09<% Foreach (var color in ViewData ["listColors"] as List<String>) {%>
- 10<Li>
- 11<FontColor = "<%: color %>"> <%: Color %> </Font>
- 12</Li>
- 13<%} %>
- 14</Ul>
- 15<P>
- 16<%: ViewData ["dateNow"] %>
- 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?
- Public ActionResult Index ()
- 02 {
- 03 List <string> colors = new List <string> ();
- 04 colors. Add ("red ");
- 05 colors. Add ("green ");
- 06 colors. Add ("blue ");
- 07
- 08 ViewBag. ListColors = colors; // colors is List
- 09 ViewBag. DateNow = DateTime. Now;
- 10 ViewBag. Name = "Hajan ";
- 11 ViewBag. Age = 25;
- 12 return View ();
- 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?
- <P>
- My name is
- <B> <%: ViewBag. Name %> </B>,
- <B> <%: ViewBag. Age %> </B>Years old.
- <Br />
- I like the following colors:
- </P>
- <UlId = "colors">
- <% Foreach (var color in ViewBag. ListColors) {%>
- <Li>
- <FontColor = "<%: color %>"> <%: Color %> </Font>
- </Li>
- <%} %>
- </Ul>
- <P>
- <%: ViewBag. DateNow %>
- </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.