Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a little confused about the IEnumerable in this case. Does it accurately depict List<Class1>?

The error is in the "foreach" statement only the "foreach" is red underlined but not the "item" or the "Model".
Error Code:
foreach statement cannot operate on variables of type 'System.Collections.Generic.IEnumerator<SimpleMVC5.Models.Class1>' because 'System.Collections.Generic.IEnumerator<SimpleMVC5.Models.Class1>' does not contain a public definition for 'GetEnumerator'   c:\Users\Optiplex760\Documents\a  ASP.net 4.5\csharp\a MVC\SimpleMVC5\SimpleMVC5\Views\Home\Index.cshtml

Index.cshtml
HTML
@model IEnumerable<SimpleMVC5.Models.Class1>
        <table>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.idt</td>
                    <td>@item.datetime0</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; }
    }

web.config
XML
<connectionStrings>
    <add name="testContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\SimpleMVC5db.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
</connectionStrings>

And SimpleMVC5db.mdf is defined and is populated with data.

If I debug it on line "lst = db.Data1.ToList();". Error code:
An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:
Posted
Updated 22-Jan-15 1:53am
v7
Comments
Nathan Minier 22-Jan-15 7:14am    
Class1.datetime0 in your definition, but Class1.datetime in your markup. Which is correct?
teledexterus 22-Jan-15 7:52am    
datetime0 but does not change the error.
Nathan Minier 22-Jan-15 8:01am    
I think EF is having trouble determining what your primary key is on Class1. EF will look for properties named Id or classnameId, but idt doesn't fit that pattern. Try to either change "idt" to "id" or attribute it with [Key].

Everything else looks pretty solid.
Zoltán Zörgő 22-Jan-15 14:57pm    
any progress?

1 solution

Class1 has no key defined. Keys are either identified by convention, by attribute or fluent api: Create Primary Key using Entity Framework Code First[^]

Try:
C#
public class Class1
    {
        [Key]
        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; }
    }
 
Share this answer
 

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