public async Task <ActionResult> Edit(ProductView view) { if (ModelState.IsValid) { var product = this.ToProductView(view); var pic = view.ImagePath; var folder = "~/Content/Products"; if (view.LifeLogo != null) { pic = FilesHelper.UploadPhoto(view.LifeLogo, folder, string.Format("{0}", product.BarCode), string.Format("{0}", product.ProductId)); view.ImageMimeType = view.LifeLogo.ContentType; int length = view.LifeLogo.ContentLength; byte[] buffer = new byte[length]; view.LifeLogo.InputStream.Read(buffer, 0, length); view.ImagenProduct = buffer; product.ImagenProduct = buffer; } using (var transaction = this.db.Database.BeginTransaction()) { this.db.Entry(product).State = EntityState.Modified; try { await this.db.SaveChangesAsync(); return(RedirectToAction("Index")); } catch (Exception ex) { transaction.Rollback(); if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { ModelState.AddModelError(string.Empty, "There are a record with the same value"); } else { ModelState.AddModelError(string.Empty, ex.Message); string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message); message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty)); ModelState.AddModelError(string.Empty, message); } } } } ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(), "CategoryId", "Description", view.CategoryId); return(View(view)); }
// GET: Products/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = db.Products.Find(id); if (product == null) { return(HttpNotFound()); } ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(product.CompanyId), "CategoryId", "Description", product.CategoryId); ViewBag.TaxId = new SelectList(ComboHelper.GetTaxes(product.CompanyId), "TaxId", "Description", product.TaxId); return(View(product)); }
// GET: Products/Edit/5 public async Task <ActionResult> Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = await this.db.Products.FindAsync(id); if (product == null) { return(HttpNotFound()); } var ViewProduct = this.ToProduct(product); ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(), "CategoryId", "Description", product.CategoryId); return(View(ViewProduct)); }
// GET: Products/Create public ActionResult Create() { var user = db .Users .Where(u => u.UserName == User.Identity.Name) .FirstOrDefault(); if (user == null) { return(RedirectToAction("Index", "Home")); } var view = new ProductView { UserId = user.UserId, }; ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(), "CategoryId", "Description"); return(View(view)); }
public ActionResult Create([Bind(Include = "ProductId,CompanyId,Description,BarCode,TaxId,CategoryId,Price,Image,Remarks")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } var user = db.Users.Where(u => u.Email == User.Identity.Name).FirstOrDefault(); if (user == null) { return(RedirectToAction("Index", "Home")); } ViewBag.CategoryId = new SelectList(ComboHelper.GetCategories(user.CompanyId), "CategoryId", "Description", product.CategoryId); ViewBag.TaxId = new SelectList(ComboHelper.GetTaxes(user.CompanyId), "TaxId", "Description", product.TaxId); return(View(product)); }