【问题标题】:Error: foreach statement cannot operate on variables of type IENumerable错误:foreach 语句不能对 IENumerable 类型的变量进行操作
【发布时间】: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; }
    }
}

我确信这可能有一个简单的解决方案,但我完全按照这本书的方式进行操作,有点生气,以至于我这么早就犯了一个错误!

如果有人能帮助我,我将不胜感激。

【问题讨论】:

    标签: asp.net-mvc ienumerable


    【解决方案1】:

    IENumerable 更改为IEnumerable

    【讨论】:

    • 完美。不敢相信这是多么愚蠢!非常感谢 :) 一旦时间限制结束,我会将其标记为正确。
    【解决方案2】:

    试试IEnumerable 而不是IENumerable

    【讨论】:

      【解决方案3】:

      我认为你有一个错字。使用 IEnumerable。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-29
        • 2019-02-22
        • 1970-01-01
        相关资源
        最近更新 更多