public async Task <IActionResult> AddModel(ModelBrandViewModel model) { if (this.ModelState.IsValid) { try { await _brandRepository.AddModelAsync(model); var brandId = model.BrandId; return(RedirectToAction($"Details/{brandId}")); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { if (ModelState.IsValid) { ModelState.AddModelError(string.Empty, $"There is allready a Model registered with the name {model.Name} please insert another"); } } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } ViewBag.Id = model.BrandId; return(this.View(model)); }
public async Task AddModelAsync(ModelBrandViewModel model) { var brand = await GetBrandWithModelsAsycn(model.BrandId); if (brand == null) { return; } brand.Models.Add(new BrandModel { ModelName = model.Name }); _context.Brands.Update(brand); await _context.SaveChangesAsync(); }
public async Task <IActionResult> AddModel(int?id) { if (id == null) { return(NotFound()); } var brand = await _brandRepository.GetByIdAsync(id.Value); if (brand == null) { return(NotFound()); } var model = new ModelBrandViewModel { BrandId = brand.Id, }; ViewBag.Id = model.BrandId; return(View(model)); }