您的位置:首页 > 其它

mvc 使用强类型来引用数据

2011-02-19 13:28 148 查看
controller:中的代码:

public ActionResult StrongType()
        {
              List<OP_CashCoupon> op = new List<OP_CashCoupon>();
            for (int i = 0; i < 10; i++)
            {
                op.Add(new OP_CashCoupon {cashCouponID="3"});
            }
            return View(op);
        }




在view页中的代码:



<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<test.Models.OP_CashCoupon>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	StrongType
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>StrongType</h2>
     <% foreach( var s in Model){ %>
        <li><%=s.cashCouponID %></li>
    <%} %>
</asp:Content>


Inherits="System.Web.Mvc.ViewPage<IEnumerable<test.Models.OP_CashCoupon>>这是引用强类型的关键。



<% foreach( var s in Model){ %>此句中的model代表的是IEnumerable<test.Models.OP_CashCoupon>类型的变量。

这样就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐