开发者

Error: foreach statement cannot operate on variables of type IENumerable

开发者 https://www.devze.com 2023-01-22 08:14 出处:网络
I\'m following the sportsStore tutorial from the Apress Pro Mvc 2 Framework book. It\'s been a while since I touched .net, and I\'m completely new to Mvc and I\'ve fallen at the first stumbling block

I'm following the sportsStore tutorial from the Apress Pro Mvc 2 Framework book.

It's been a while since I touched .net, and I'm completely new to Mvc and I've fallen at the first stumbling block!

I have the following error:

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

Here is the code for the View:

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

Here is the snippet from the controller:

public ViewResult List() { return View(productsRepository.Products.ToList()); }

And here is the product class:

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; }
    }
}

I'm sure this has 开发者_如何学Cprobably a simple solution but I've followed the book exactly so slightly peeved that I've made a mistake so early on!

If anyone could help me out I'd be very grateful.


Change IENumerable to IEnumerable.


Try IEnumerable not IENumerable.


I think you have a typo. Use IEnumerable.

0

精彩评论

暂无评论...
验证码 换一张
取 消