ViewData的使用

weixin_34304013發表於2012-07-24

//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%>