public ActionResult Edit(EditProductFormModel model) { if (ModelState.IsValid) { var sanpham = db.SANPHAMs.FirstOrDefault(m => m.MaSP == model.MaSP); if (sanpham != null) { sanpham.MaSP = model.MaSP; sanpham.TenSP = model.TenSP; sanpham.MoTa = model.MoTa; //chua chinh anh sanpham.ChiTiet = model.ChiTiet; sanpham.GiaSP = model.GiaSP; sanpham.SoLuong = model.SoLuong; sanpham.MaDM = model.MaDM; if (model.ProductImage != null) { var fileName = model.ProductImage.FileName; var link = "/uploads/" + fileName; var real = Server.MapPath("~" + link); model.ProductImage.SaveAs(real); sanpham.Anh = link; } db.SaveChanges(); } return(RedirectToAction("Index", "Product")); } return(View(model)); }
public IActionResult Edit(EditProductFormModel model) { if (this._categories.NameById(model.CategoryId) == null) { TempData.AddErrorMessage("Невалидна категория."); return(RedirectToAction("Index", "Home", new { area = "" })); } var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); string uniqueFileName = ""; bool image = false; if (model.Image != null) { if (model.Image.Length > 0) { uniqueFileName = this.GetUniqueName(model.Image.FileName); var filePath = Path.Combine(uploads, uniqueFileName); model.Image.CopyTo(new FileStream(filePath, FileMode.Create)); image = true; } } string oldImagePath = this._products.Details(model.Id).Image; if (oldImagePath != "default.jpg") { this.DeleteOldImage(oldImagePath); } if (image) { var result = this._products.Update(model.Id, model.Title, model.Price, model.Description, uniqueFileName, model.CategoryId); } else { var result = this._products.Update(model.Id, model.Title, model.Price, model.Description, oldImagePath, model.CategoryId); } TempData.AddSuccessMessage("Успешно редактирахте продукта."); return(RedirectToAction("Index", "Home", new { area = "" })); }
public ActionResult EditProduct(EditProductFormModel inputModel) { if (ModelState.IsValid) { var domainModel = Mapper.Map <ProductDomainModel>(inputModel); if (product.Update(domainModel)) { return(RedirectToAction("Index")); } } var viewModel = Mapper.Map <EditProductViewModel>(inputModel); IEnumerable <CategoryDomainModel> categories = category.Get(); viewModel.Categories = Mapper.Map <List <CategoryViewModel> >(categories); return(View(viewModel)); }
public ActionResult Edit(int masp /*,HttpPostedFileBase ProductImage*/) { var sanpham = db.SANPHAMs.FirstOrDefault(m => m.MaSP == masp); if (sanpham == null) { RedirectToAction("Index", "Product"); } var chinhsua = new EditProductFormModel(); chinhsua.TenSP = sanpham.TenSP; //chinhsua.ProductImage = sanpham.ProductImage; chinhsua.MoTa = sanpham.MoTa; chinhsua.ChiTiet = sanpham.ChiTiet; chinhsua.GiaSP = sanpham.GiaSP; chinhsua.SoLuong = sanpham.SoLuong; chinhsua.MaDM = sanpham.MaDM; chinhsua.hinhCu = sanpham.Anh; return(View(chinhsua)); }