Beispiel #1
0
        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));
        }
Beispiel #2
0
        /// <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));
        }