Reputation: 33
I can get my Products model to my view ok. but I need to pass it in a ViewModel so I can interact with data from several models. When I bind my view to ProductPageViewModel then I get the "no public definition for GetEnumerator" error. I have tried all manner of IEnumerable options and cannot find the right combination.
I included controller and view that work when the view is bound to the model.
The full error is
CS1579: foreach statement cannot operate on variables of type JaniesWebLive.ViewModel.ProductPageViewModel' because JaniesWebLive.ViewModel.ProductPageViewModel' does not contain a public definition for 'GetEnumerator'
Triggered at @foreach (var item in Model)
Products.cs
public class Products
{
[Key]
public int WpId { get; set; }
public string Category { get; set; }
public string WpProductId { get; set; }
public string ProdDescShort { get; set; }
public string ProdDescLong { get; set; }
public string ProductMedium { get; set; }
public decimal? Price1 { get; set; }
}
ProductPageViewModel.cs
public class ProductPageViewModel
{
public IEnumerable<Products> Products { get; set; }
public Contact Contacts { get; set; }
public string Message { get; set; }
/*more models to be added */
}
ProductsController.cs
public class ProductsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult ShortList(string prodname)
{
var products = db.DbProducts.Where(m => m.Category == "homepage");
var model = new ViewModel.ProductPageViewModel
{
Products = products,
Message = "Thanks for your business! "
/* more models to be added */
};
return View(model);
}
}
ShortList.cshtml
@model IEnumerable<JaniesWebLive.ViewModel.ProductPageViewModel>
@foreach (var item in Model)
{
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-12">
<p><img class="product-image" [email protected](modelItem => item.ProductMedium) /></p><br />
<h3>@Html.DisplayFor(modelItem => item.WpProductId)</h3>
<h5>@Html.DisplayFor(modelItem => item.Price1)</h5>
<h6>@Html.DisplayFor(modelItem => item.ProdDescShort)</h6>
<p class="text-center">@Html.DisplayFor(modelItem => item.ProdDescLong)</p>
<p>
<button class="text-right btn btn-link">SHORT LIST</button>
</p>
</div>
</div>
}
Working controller and view
public ActionResult LongList(string prodname)
{
var model = db.DbProducts.Where(m => m.Category == "homepage");
return View(model);
}
LongList.cshtml
@model JaniesWebLive.Models.Products
@foreach (var item in Model)
{
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-12">
<p><img class="product-image" [email protected](modelItem => item.ProductMedium) /></p>
<h3>@Html.DisplayFor(modelItem => item.WpProductId)</h3>
<h5>@Html.DisplayFor(modelItem => item.Price1)</h5>
<h6>@Html.DisplayFor(modelItem => item.ProdDescShort)</h6>
<p class="text-center">@Html.DisplayFor(modelItem => item.ProdDescLong)</p>
<p>
<button class="text-right btn btn-link">LONG LIST</button>
</p>
</div>
</div>
}
Upvotes: 2
Views: 1333
Reputation: 1579
you need to change the view to something like
@model JaniesWebLive.ViewModel.ProductPageViewModel
@foreach (var item in Model.Products )
{
<div class="row">
<div class="col-md-4 col-sm-6 col-xs-12">
<p><img class="product-image" [email protected](modelItem => item.ProductMedium) /></p><br />
<h3>@Html.DisplayFor(modelItem => item.WpProductId)</h3>
<h5>@Html.DisplayFor(modelItem => item.Price1)</h5>
<h6>@Html.DisplayFor(modelItem => item.ProdDescShort)</h6>
<p class="text-center">@Html.DisplayFor(modelItem => item.ProdDescLong)</p>
<p>
<button class="text-right btn btn-link">SHORT LIST</button>
</p>
</div>
</div>
}
since upr view model is a single object which contain a list property you need to itterate the property not the whole model
Upvotes: 0