I'm new to asp.net mvc and I'm doing this exercise for myself. I created an edmx from Northwind Database. I created a controller:
public ActionResult Index(int id)
{
var model = new IndexViewModel();
using (var db = new ProductDB())
{
model.Products = from p in db.Products
orderby p.ProductName
select new IndexViewModel.InfoProduct
{
ProductName = p.ProductName,
QuantityPerUnit = p.QuantityPerUnit,
UnitPrice = p.UnitPrice
};
}
return View();
}
...the view:
@model aspTest.Models.IndexViewModel
@{
Layout = null;
}
...
<div> <ul>
@foreach (var p in Model.Products){
<li>@p.ProductName</li>
}
</ul>
</div>
...and the ViewModel:
public class IndexViewModel
{
public IEnumerable<InfoProduct> Products { get; set; }
public class InfoProduct
{
public string ProductName { get; set; }
}
}
But this error keeps on appearing in this part: @foreach (var p in Model.Products){
Sorry, I know this might be noobish to most of you.