public IActionResult Create([FromServices] IMakeRepo makeRepo, Car car) { if (ModelState.IsValid) { _repo.Add(car); return(RedirectToAction(nameof(Details), new { id = car.Id })); } ViewData["MakeId"] = GetMakes(makeRepo); return(View(car)); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public IActionResult OnPost() { if (ModelState.IsValid) { _carRepo.Add(Car); return(RedirectToPage("./Index")); } Makes = GetMakes(); return(Page()); }
public IActionResult Create([Bind("CarSize, LicensePlate, DistanceInKm")] Car car) { car.IsBooked = false; bool isCarNew = _carRepo.CheckIfCarIsOnDatabase(car); if (isCarNew) { ViewBag.error = "Bilen med detta reg-nummret finns redan"; return(View("~/Views/Home/Index.cshtml")); } if (ModelState.IsValid) { _carRepo.Add(car); ViewBag.ok = $"Bilen med reg-nummer {car.LicensePlate} är tillagd"; return(View("~/Views/Home/Index.cshtml")); } return(View("~/Views/Car/CreateNewCar.cshtml")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. //TODO: Slides Change action name https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio public IActionResult OnPostCreateNewCar() { if (!ModelState.IsValid) { Makes = GetMakes(); return(Page()); } try { _carRepo.Add(Car); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); Makes = GetMakes(); return(Page()); } return(RedirectToPage("./Index")); }