Example - Using ViewBag
Controller
public
ActionResult Index()
{
List< string> colors = new List< string>();
colors.Add( "red");
colors.Add( "green");
colors.Add( "blue");
ViewBag.ListColors = colors; //colors is List
ViewBag.DateNow = DateTime.Now;
ViewBag.Name = "Hajan";
ViewBag.Age = 25;
return View();
}
{
List< string> colors = new List< string>();
colors.Add( "red");
colors.Add( "green");
colors.Add( "blue");
ViewBag.ListColors = colors; //colors is List
ViewBag.DateNow = DateTime.Now;
ViewBag.Name = "Hajan";
ViewBag.Age = 25;
return View();
}
You see the difference?
View (ASPX View Engine)
<
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 >
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 >