SCS
SCS

Reputation: 639

foreach GetEnumerator error when converting to Razor syntax

I'm currently in the process of converting the MVC 2 portion of a hybrid Web Forms/MVC 2 application to MVC 3 (I'm not familiar with Web Forms or MVC 2) and have been encountering some problems with some of the foreach statements (which work fine in the original hybrid application).

When converting to Razor, some (not all) of my foreach statements will say something along the lines of:

foreach statement cannot operate on variables of type 'System.Data.Objects.DataClasses.EntityCollection<Project.Activity.PortalMember>' because 'System.Data.Objects.DataClasses.EntityCollection<Project.Activity.PortalMember>' does not contain a public definition for 'GetEnumerator'

This is an example .ascx from the Web Forms/MVC 2 application where the foreach works fine:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Project.Activity.PortalGroup>" %>
<% if (Model.PortalMembers.Count() > 0) {  %>
    <div>
    <% foreach (var item in Model.PortalMembers) {  %>
        <div>
            <%: item.SubAccount.FirstName %> <%: item.SubAccount.LastName %>
        </div>
    <%} %>
    </div>
<%} %>

This is my attempt for the .cshtml conversion that is giving me the error:

@model Project.Activity.PortalGroup

@if (Model.PortalMembers.Count() > 0) 
{
    <div>
    @foreach (var item in Model.PortalMembers) 
    {
        <div>
            @item.SubAccount.FirstName @item.SubAccount.LastName
        </div>
    }
    </div>
}

Upvotes: 3

Views: 2763

Answers (2)

Arafat Hegazy
Arafat Hegazy

Reputation: 31

Try to add reference to System.Data.Entity.

Upvotes: 0

Dima Pasko
Dima Pasko

Reputation: 1170

Try add reference to System.Data.Linq.dll in your project

Upvotes: 2

Related Questions