ViewData的使用
//Controller
public ActionResult Index() { ViewData["Message"] = "欢迎使用 ASP.NET MVC!"; List<string> list= new List<string>(); list.Add("test1"); list.Add("test2"); ViewData["list"] = list; Models.User user = new Models.User(); user.ID = 100; user.Name = "ZG"; ViewData["User"] = user; return View(); }
<%= Html.Encode(ViewData["Message"]) %></h2> <%foreach (var item in ViewData["list"] as List<string>) %> <% { %> <% =item %> <%} %> <br /> <% var user = ViewData["User"] as MvcDemo.Models.User;%> <%=user.Name%> <%=user.ID%>