public IActionResult Add(AddCarWithPartsModel carWithParts) { if (!this.ModelState.IsValid) { return(this.NotFound()); } this.carService.AddCarWithParts(carWithParts); return(this.RedirectToAction("CarsWithParts")); }
public IActionResult Add() { var parts = this.partService.AllPartNamesAndPrices(); var carWithParts = new AddCarWithPartsModel { Parts = parts }; return(this.View(carWithParts)); }
public void AddCarWithParts(AddCarWithPartsModel carWithParts) { Car newCar = new Car { Make = carWithParts.Make, Model = carWithParts.Model, TravelledDistance = carWithParts.TravelledDistance }; var choosenPartsIds = this.GetSelectedPartsIds(carWithParts.PartName, carWithParts.PartNameTwo, carWithParts.PartNameThree); foreach (var id in choosenPartsIds) { newCar.Parts.Add(new PartCar { CarId = newCar.Id, PartId = id }); } this.db.Cars.Add(newCar); this.db.SaveChanges(); }