2010-10-29 46 views
1

我正在關注Apress Pro Mvc 2 Framework書中的體育商店教程。錯誤:foreach語句無法對IENumerable類型的變量操作

自從我接觸.net,我已經有一段時間了,而我對Mvc來說是全新的,而我已經落在第一個絆腳石!

我有以下錯誤:

foreach statement cannot operate on variables of type 'IENumerable' because 'IENumerable' does not contain a public definition for 'GetEnumerator'

下面是查看代碼:)

公衆的ViewResult列表( :

<%@ 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> 

這裏是從控制器片段{} {返回View(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; } 
    } 
} 

我敢肯定,這有可能是一個簡單的解決辦法,但我跟着這本書正是這樣略微惱怒,我犯了一個錯誤這麼早上!

如果有人能幫助我,我會非常感激。

+0

+1這本書! :-) – DaveDev 2010-10-29 22:54:05

回答

4

IENumerable更改爲IEnumerable

+0

完美。不能相信這是多麼愚蠢!非常感謝:)一旦時間限制結束,我會將其標記爲正確。 – 109221793 2010-10-29 14:32:41

2

嘗試IEnumerable不是IENumerable

1

我認爲你有一個錯字。使用IEnumerable。

相關問題