コード例 #1
0
        public IActionResult AddMakeAndModel()
        {
            var makes = this.makeCarService.GetAll <MakeInputViewModel>().ToList();

            makes.Add(new MakeInputViewModel {
                Id = 999999999, Name = "Other"
            });
            var viewModel = new AddMakeAndModelViewModel()
            {
                Makes = makes,
            };

            return(this.View(viewModel));
        }
コード例 #2
0
        public async Task <IActionResult> AddMakeAndModel(AddMakeAndModelViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                var makes = this.makeCarService.GetAll <MakeInputViewModel>().ToList();
                makes.Add(new MakeInputViewModel {
                    Id = 999999999, Name = "Other"
                });
                input.Makes = makes;
                return(this.View(input));
            }

            var inputMakeName  = input.Make;
            var inputModelName = input.Model;
            var isMakeId       = int.TryParse(inputMakeName, out int makeId);

            if (isMakeId)
            {
                await this.modelCarService.AddAsync(inputModelName, makeId);
            }
            else
            {
                if (this.makeCarService.IsHasMakeName(inputMakeName)) //if user is selected "other", but he entered existing makeName
                {
                    var make = this.makeCarService.GetByName <MakeInputViewModel>(inputMakeName);
                    await this.modelCarService.AddAsync(inputModelName, make.Id);

                    makeId = make.Id;
                }
                else
                {
                    makeId = await this.makeCarService.AddAsync(inputMakeName);

                    await this.modelCarService.AddAsync(inputModelName, makeId);
                }
            }

            return(this.RedirectToAction(nameof(this.Details), "Makes", new { Id = makeId }));
        }