MVC 3
ViewData: There are some points about it describe below
public ActionResult Edit(int id) { Dinner dinner = dinnerRepository.GetDinner(id); ViewData["Countries"] = new SelectList(PhoneValidator.AllCountries, dinner.Country);return View(dinner); } <%= Html.DropDownList("Country", ViewData["Countries"] as SelectList) %>
ViewBag:
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 = "Raj"; ViewBag.Age = 25; return View(); } <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>
TempData:
public void Create(int id) { Product prod = new Product(); prod.UpdateFrom(Request.From); if(prod.Validate()) { prod.Save(); RedirectToAction("List"); } else { TempData["ErrorMessage"] = "Invalid entry. Try again."; RedirectToAction("New"); } } //New.aspx: <% if(TempData.ContainsKey("ErrorMessage") { %> Error happened: <%= TempData["ErrorMessage"] %> <% } %> rest of the HTML ...
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: