public async Task <IActionResult> Create(AddSpareBrandViewModel model) { if (ModelState.IsValid) { var sparebrand = await _context.SpareBrands.FirstOrDefaultAsync (sb => sb.Name.ToUpper() == model.Name.ToUpper()); if (sparebrand != null) { var error = "The Spare Brand can't be Added because it already exists."; return(RedirectToAction("Index", new RouteValueDictionary(new { Controller = "SpareBrands", error }))); } var path = string.Empty; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile, "SpareBrands"); } sparebrand = _converterHelper.ToSpareBrandEntity(model, path); await _context.SpareBrands.AddAsync(sparebrand); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public SpareBrandEntity ToSpareBrandEntity(AddSpareBrandViewModel model, string path) { return(new SpareBrandEntity { Id = model.Id, Name = model.Name, ImageUrl = path }); }
public async Task <IActionResult> Edit(int id, AddSpareBrandViewModel model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { var path = model.ImageUrl; if (model.ImageFile != null) { path = await _imageHelper.UploadImageAsync(model.ImageFile, "SpareBrands"); //TODO: investigar borrar imagen anterior _imageHelper.DeleteImageAsync(model.ImageUrl); } var sparebrand = _converterHelper.ToSpareBrandEntity(model, path); try { _context.SpareBrands.Update(sparebrand); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpareBrandEntityExists(model.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var model = await _context.SpareBrands.FindAsync(id); if (model == null) { return(NotFound()); } var spareBrandviewmodel = new AddSpareBrandViewModel { Id = model.Id, BrandCategories = model.BrandCategories, ImageUrl = model.ImageUrl, Name = model.Name }; return(View(spareBrandviewmodel)); }