public IActionResult Create(CarCreatorViewModel postedModel) { if (ModelState.IsValid) { var entity = postedModel.CreateEntity(); this.context.Cars.Add(entity); this.context.SaveChanges(); return(RedirectToAction("Index", "Home")); } postedModel.Brands = context.Brands.ToDictionary(b => b.Id, b => b.Name); return(View(postedModel)); }
/// <summary> /// Navrátí formulář /// </summary> public IActionResult Create(int?preselectedBrand = null) { var model = new CarCreatorViewModel(); model.Brands = context.Brands.ToDictionary(b => b.Id, b => b.Name); // Pokud je předem vybraná značka if (preselectedBrand != null) { var brand = this.context.Brands.FirstOrDefault(b => b.Id == preselectedBrand.Value); // a existuje if (brand != null) { // přidám ji do modelu model.BrandId = preselectedBrand.Value; } } return(View(model)); }