asp.net mvc - how to write method for mapping lists in viewmodel and display collection in razor view -
i trying display collection of products (using view model) in pro()
method
models
public class product { public product() { this.adddate = datetime.now; } public int productid { get; set; } [required(errormessage = "enter product name")] [stringlength(160)] public string productname { get; set; } [required(errormessage = "enter product description")] [stringlength(500)] public string productdescription { get; set; } public datetime adddate { ; private set; } [required(errormessage = "select category")] [display(name="category")] public string category { get; set; } [required(errormessage = "location")] public bool location { get; set; } } public class productpicture { public int id { get; set; } public int productid { get; set; } [required(errormessage ="please select picture")] public string pictureurl { get; set; } }
view model
public class productviewmodel { public int productid { get; set; } public string productname { get; set; } [required(errormessage = "enter product description")] [stringlength(500)] public string productdescription { get; set; } public datetime adddate { get; private set; } [required(errormessage = "select category")] [display(name = "category")] public string category { get; set; } [required(errormessage = "location")] public bool location { get; set; } public ienumerable<productpicture> productpictures { get; set; } }
controller method
public actionresult pro() { productviewmodel product = new productviewmodel(); var pro = db.products.tolist(); if (pro != null) { product.productname = pro.productname; product.productdescription = pro.productdescription; product.payextra = pro.payextra; product.location = pro.location; product.productpictures = db.productpictures.where(m => m.productid == pro.select(m=>m.productid)); } return view(product); }
currently throwing following exception
since trying map complete list single object "=" operator can not applied int ienumerable. logically doing wrong , please me select current id ienumerable , map current productpicture list , @ last create collection of same viewmodel can displayed in view.
you need change method project collection of product
collection of productviewmodel
, return view.
public actionresult pro() { var pictures = db.productpictures; var model = db.products.select(p => new productviewmodel { productname = p.productname, productdescription = p.productdescription, .... productpictures = pictures.where(x => x.productid == p.id) }; return view(model); }
then in view
@model ienmerable<productviewmodel> <table> <thead> <tr> <th>@html.displaynamefor(m => m.productname)</th> .... </tr> </thead> <tbody> @foreach(var product in model) { <tr> <td>@html.displayfor(m => product.productname)</td> ....
side note: recommend change name of method index()
means user can navigate using .../products
(assuming using default routing), or @ least name indicates displaying products (the current url ../products/pro
not convey useful meaning user)
Comments
Post a Comment