简体   繁体   中英

Error: Doesn't contain a public definition of GetEnumerator

When I am trying to run the application I am getting this error constantly ie

"CS1579: foreach statement cannot operate on variables of type 'Models.FloorPlanViewModel' because 'Models.FloorPlanViewModel' does not contain a public definition for 'GetEnumerator'"

its crashing at foreach of SummaryTable.cshtml

Line 34:             </tr>
Line 35: 
Line 36:             @foreach (var item in Model)
Line 37:             {
Line 38:                 <tr>

Below is FloorPlanViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using USTGlobal.WorkBench.Repository;

namespace UI.Models
{
    public class FloorPlanViewModel
    {

        public IEnumerable<SummaryConfiguration> sumConfig { get; set; }



    }

}

Any help plz?

@foreach (var item in Model.sumConfig)

Just to add a more detailed explanation.

When we can use a foreach?
The foreach statement is used to iterate through an object collection that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface.
foreach, in (C# Reference)

Why your code does not work?

@foreach (var item in Model)    

Your want iterate through Model , but it does not implement IEnumerable.

Why the code of the accepted answer works?

@foreach (var item in Model.sumConfig)

Because Model.sumConfig is of type IEnumerable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM