I have created a ViewModel for a Product Edit page because it has specific related entities I need to load - the class currently looks like this:
public class ProductEditView
{
public ProductEditView(Product product)
{
this.ID = product.ID;
this.Code = product.Code;
this.ProductLanguage = product.ProductLanguages.Where(x => x.LanguageID == Global.Language).FirstOrDefault();
this.PrimaProduct = product.PrimaProduct;
this.Multimedias = product.Multimedias;
}
public int ID { get; set; }
public string Code { get; set; }
public ProductLanguage ProductLanguage { get; set; }
public PrimaProduct PrimaProduct { get; set; }
public IEnumerable<Multimedia> Multimedias { get; set; }
}
So then in my controller I can go:
public ActionResult Edit(int? id)
{
if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null) return HttpNotFound();
ProductEditView productEditView = new ProductEditView(product);
return View(productEditView);
}
I've not really seen anybody do it like this though, is it bad practice - and for what reason? What are the better alternatives to accomplish this if this is wrong?