mvc 使用强类型来引用数据

    技术2026-06-15  10

    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>类型的变量。

    这样就行了。

    最新回复(0)