Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am a little confused about the IEnumerator in this case. Does it accurately depict List<class1>?

The error is in the "foreach" statement only the "foreach" is red underlined not the "item" or the "Model".

It says it "...does not contain a public definition for 'GetEnumerator...'

Index.cshtml
C#
@model IEnumerator<SimpleMVC5.Models.Class1>
        <table>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.idt</td>
                    <td>@item.datetime</td>
                    <td>@item.col1</td>
                    <td>@item.col2</td>
                    <td>@item.col3</td>
                </tr>
            }
        </table>

HomeController.cs
C#
public class HomeController : Controller
    {
        private testContext db = new testContext();
        List<Class1> lst = new List<Class1>();
        public ActionResult Index()
        {
            lst = db.Data1.ToList();
            return View(lst);
        }

Class1.cs
C#
public class Class1
    {
        public int idt { get; set; }
        public string datetime0 { get; set; }
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
    public class testContext : DbContext
    {
        public DbSet<Class1> Data1 { get; set; }
    }
Posted
Updated 21-Jan-15 12:20pm
v2

1 solution

It clearly tells you that the object Model cannot be used as a set of object for using in foreach. This is simple: the type of such object should support System.Collections.IEnumerable:
https://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.110%29.aspx[^].

Pay attention: IEnumerable, not IEnumerator! You would need to implement IEnumerator by the enumerator, the object returned by System.Collections.IEnumerable.GetEnumerator.

Yes, collections generally implement System.Collections.IEnumerable. When in doubt, look at the MSDN help page if the type in question and check it up; this detail is always shown. You did not show the type of your Model object, but it is certainly not a collection and is not derived from any collection type, but probably one of its members is of collection type, so, as in most cases, you probably still won't need to implement this interface.

—SA
 
Share this answer
 
Comments
Member 11259413 11-Dec-15 12:07pm    
Why is it that whenever someone submits an answer to someone's question on here they are always a f***ing a**hole about it?

He's looking for help not snide comments that make him feel stupid. dick
dking5454 2021 28-May-21 2:25am    
reading through SA's reply, I was thinking the exact same thing!
[no name] 11-Dec-15 15:45pm    
A 5, see my comments on the comment in respective section.
Sergey Alexandrovich Kryukov 11-Dec-15 15:53pm    
Thank you, Bruno.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900