【发布时间】:2010-10-29 14:21:36
【问题描述】:
我正在学习 Apress Pro Mvc 2 框架一书中的 sportsStore 教程。
我接触 .net 已经有一段时间了,我对 Mvc 完全陌生,遇到了第一个绊脚石!
我有以下错误:
foreach 语句无法操作 类型变量 'IENumerable' 因为 'IENumerable' 不包含公共定义 对于'GetEnumerator'
这是视图的代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IENumerable<SportsStore.Domain1.Entities.Product>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Products
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Products</h2>
<% foreach (var product in Model)
{ %>
<div class="item">
<h3><%: product.Name%></h3>
<%: product.Description%>
<h4><%: product.Price.ToString("c")%></h4>
</div>
<% } %>
</asp:Content>
这是来自控制器的 sn-p:
公共查看结果列表() { 返回视图(productsRepository.Products.ToList()); }
这是产品类别:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SportsStore.Domain1.Entities
{
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
}
我确信这可能有一个简单的解决方案,但我完全按照这本书的方式进行操作,有点生气,以至于我这么早就犯了一个错误!
如果有人能帮助我,我将不胜感激。
【问题讨论】: