コード例 #1
0
 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));
 }
コード例 #2
0
 // 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());
 }
コード例 #3
0
        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"));
        }
コード例 #4
0
ファイル: Create.cshtml.cs プロジェクト: wddot/presentations
        // 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"));
        }