// GET: Products/Create public ActionResult Create() { var user = db.Users. Where(us => us.UserName == User.Identity.Name). FirstOrDefault(); ViewBag.CategoryId = new SelectList( ComboBoxHelpers. GetCategories( user.CompanyId), "CategoryId", "Description"); ViewBag.TaxId = new SelectList( ComboBoxHelpers. GetTaxes( user.CompanyId), "TaxId", "Description"); var product = new Product { CompanyId = user.CompanyId, }; return(View(product)); }
// GET: Products/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult( HttpStatusCode.BadRequest)); } var product = db.Products.Find(id); if (product == null) { return(HttpNotFound()); } var user = db.Users. Where(us => us.UserName == User.Identity.Name). FirstOrDefault(); ViewBag.CategoryId = new SelectList( ComboBoxHelpers. GetCategories( user.CompanyId), "CategoryId", "Description", product.CategoryId); ViewBag.TaxId = new SelectList( ComboBoxHelpers. GetTaxes( user.CompanyId), "TaxId", "Description", product.TaxId); return(View(product)); }
public ActionResult Edit(Product product) { var user = db.Users. Where(us => us.UserName == User.Identity.Name). FirstOrDefault(); if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; if (product.ImageFile != null) { var folder = "~/Content/Products"; var pic = string.Empty; var file = string.Format("{0}.jpg", product.ProductID); var response0 = FilesHelpers. UploadPhoto( product.ImageFile, folder, file); if (response0) { pic = string.Format("{0}/{1}", folder, product.ProductID); product.Image = pic; } } var response = DBHelpers.SaveChanges(db); if (response.Succeeded) { return(RedirectToAction("Index")); } ModelState.AddModelError( string.Empty, response.Message); } ViewBag.CategoryId = new SelectList( ComboBoxHelpers. GetCategories( user.CompanyId), "CategoryId", "Description", product.CategoryId); ViewBag.TaxId = new SelectList( ComboBoxHelpers. GetTaxes( user.CompanyId), "TaxId", "Description", product.TaxId); return(View(product)); }