public ActionResult Edit(int id) { var model = new EditBrand(); try { var brand = db.Brands.Where(b => b.BrandId == id).FirstOrDefault(); if (brand != null) { model.BrandId = brand.BrandId; model.BrandName = brand.BrandName; model.Description = brand.Description; return(View(model)); } else { ModelState.AddModelError("", "Không tìm thấy nhãn hiệu cần cập nhật!"); } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); // Write error log var log = new Log(); log.LogDate = DateTime.Now; log.Action = "Brand - Edit()"; log.Tags = "Error"; log.Message = ex.ToString(); db.Logs.Add(log); } return(View(model)); }
public async Task <Brand> Update(EditBrand model) { var dbBrand = await GetById(model.Id); dbBrand.Name = model.Name; dbBrand.EditDate = DateTime.Now; if (model.CoverUpdate != null) { dbBrand.CoverId = await SaveCover(model.Id, model.Name, model.CoverUpdate); } if (model.Logo != null) { await SaveVectorLogo(model.Logo, model.Name.Replace(" ", String.Empty)); } _brandRepository.Update(dbBrand); _brandRepository.SaveChanges(); bool imagesUpdate; if (model.ImagesUpdate != null) { imagesUpdate = await SaveImages(model.Id, model.Name, model.ImagesUpdate); } return(await GetByIdWithImages(model.Id)); }
public async Task Update(EditBrand entity) { var brand = new Brand { Id = entity.Id, Name = entity.Name }; await _brandRepository.Update(brand); }
public async Task <IActionResult> EditBrand(EditBrand editedBrand) { if (!ModelState.IsValid) { return(View("~/Views/Admin/Brand/EditBrand.cshtml", editedBrand)); } var resultBrand = await _brandService.Update(editedBrand); var mappedBrand = _mapper.Map <EditBrand>(resultBrand); return(View("~/Views/Admin/Brand/EditBrand.cshtml", mappedBrand)); }
public ActionResult Edit(EditBrand model) { try { if (ModelState.IsValid) { // Was brand name existed var wasBrandExisted = db.Brands.Count(b => String.Compare(b.BrandName, model.BrandName, true) == 0 && b.BrandId != model.BrandId) > 0; if (wasBrandExisted == false) { var brand = db.Brands.Where(b => b.BrandId == model.BrandId).FirstOrDefault(); if (brand != null) { brand.BrandName = model.BrandName; brand.Description = model.Description; db.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Lưu thương hiệu thất bại! Không tìm thấy thương hiệu cần cập nhật."); } } else { ModelState.AddModelError("", "Lưu thương hiệu thất bại! Tên thương hiệu đã tồn tại trong hệ thống."); } } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); // Write error log var log = new Log(); log.LogDate = DateTime.Now; log.Action = "Brand - Edit()"; log.Tags = "Error"; log.Message = ex.ToString(); db.Logs.Add(log); } return(View(model)); }