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
@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
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
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
<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: