asp.net mvc viewmodel getting error public definition for getenumerator -
i can products model view ok. need pass in viewmodel can interact data several models. when bind view productpageviewmodel "no public definition getenumerator" error. have tried manner of ienumerable options , cannot find right combination.
i included controller , view work when view bound model.
the full error
cs1579: foreach statement cannot operate on variables of type janiesweblive.viewmodel.productpageviewmodel' because janiesweblive.viewmodel.productpageviewmodel' not contain public definition 'getenumerator'
triggered @ @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 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 business! " /* more models 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" src=@html.displayfor(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 , 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" src=@html.displayfor(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> }
you need change view
@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" src=@html.displayfor(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 single object contain list property need itterate property not whole model
Comments
Post a Comment