Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
}
like image 709
SCS Avatar asked Apr 27 '26 08:04

SCS


1 Answers

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

like image 67
Dima Pasko Avatar answered Apr 28 '26 23:04

Dima Pasko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!