1、View层

ViewDataDictionary类


View 负责输出数据而非样式

指定View
  指定View作为输出

public class HomeController
{
  public ActionResult Index()
{
  ViewData["Message"]="abc";
return View("~/url/other/view.aspx");
}

}

非强类型写法
public ActionResult List()
{
  var products=new List<Product>();
for(int i=0;i<10;i++)
{
  products.add(new Product{productName="p"+i});
}
ViewData["Products"] =products;
return View();
}

<ul>
  <% foreach(Product p in (ViewData["Products"] as Ienumerable<Product>)){%>
     <li><%:p.ProductName%></li>
<%}%>
</ul>

强类型写法
public ActionResult List()
{
  var products=new List<Product>();
for(int i=0;i<10;i++)
{
  products.add(new Product{productName="p"+i});
}
ViewData["Products"] =products;
return View();
}

----------------------------------------
<%@Page Language="c#" MasterPageFile="~/Views/Shared/Site.Master"
Inherited="System.Web.Mvc.ViewPage<IEnumerable<Product>>"%>

<ul>
  <%foreach(Product p in Model){%>
    <li><%:p.ProductName%></li>
</ul>


ViewModels

自定义ViewModels
public class ShoppingCartViewModel
{
  public List<Product> Products{get;set;}
public decimal CartTotal{get;set;}
public string Message{get;set;}
}
<%@Page Language="c#" MasterPageFile="~/Views/Shared/Site.Master"
Inherited="System.Web.Mvc.ViewPage<IEnumerable<ShoppingCartViewModel>>"%>


2011-4-13 19:55 danny

你可能感兴趣的:(C++,c,mvc,Web,C#)