diff --git a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs index 32e76e0..f079fa8 100644 --- a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs +++ b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs @@ -48,15 +48,21 @@ public IActionResult Create() } [HttpPost] + [ValidateAntiForgeryToken] public IActionResult Create(RestaurantEditModel model) { - var newRestaurant = new Restaurant(); - newRestaurant.Name = model.Name; - newRestaurant.Cuisine = model.Cuisine; + if (ModelState.IsValid) + { + var newRestaurant = new Restaurant(); + newRestaurant.Name = model.Name; + newRestaurant.Cuisine = model.Cuisine; + + newRestaurant = _restaurantData.Add(newRestaurant); - newRestaurant = _restaurantData.Add(newRestaurant); + return RedirectToAction("Details", new {id = newRestaurant.Id}); + } - return RedirectToAction("Details", new {id = newRestaurant.Id}); + return View(); } } } \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs b/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs index f9393b3..8c0c6b8 100644 --- a/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs +++ b/OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs @@ -1,8 +1,13 @@ +using System.ComponentModel.DataAnnotations; + namespace OdeToFoodRider.Models { public class Restaurant { public int Id { get; set; } + + [Display(Name = "Restaurant Name")] + [Required, MaxLength(80)] public string Name { get; set; } public CuisineType Cuisine { get; set; } } diff --git a/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs b/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs index b59c412..30657cb 100644 --- a/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs +++ b/OdeToFoodRider/OdeToFoodRider/ViewModels/RestaurantEditModel.cs @@ -1,9 +1,11 @@ +using System.ComponentModel.DataAnnotations; using OdeToFoodRider.Models; namespace OdeToFoodRider.ViewModels { public class RestaurantEditModel { + [Required, MaxLength(80)] public string Name { get; set; } public CuisineType Cuisine { get; set; } } diff --git a/OdeToFoodRider/OdeToFoodRider/Views/Home/Create.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/Home/Create.cshtml index 0909bed..3719399 100644 --- a/OdeToFoodRider/OdeToFoodRider/Views/Home/Create.cshtml +++ b/OdeToFoodRider/OdeToFoodRider/Views/Home/Create.cshtml @@ -11,13 +11,22 @@

Create

- +
+ + + +
- @*VSRD: Rider's code analysis bug in the asp-items expression below: RSRP-469518 *@ - +
+ + + +
+ +
diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs index ea08154..b8b3e61 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs @@ -52,8 +52,11 @@ public IActionResult Create() } [HttpPost] + [ValidateAntiForgeryToken] public IActionResult Create(RestaurantEditModel model) { + if(ModelState.IsValid) + { var newRestaurant = new Restaurant(); newRestaurant.Name = model.Name; newRestaurant.Cuisine = model.Cuisine; @@ -61,7 +64,11 @@ public IActionResult Create(RestaurantEditModel model) newRestaurant = _restaurantData.Add(newRestaurant); return RedirectToAction(nameof(Details), new { id=newRestaurant.Id }); - + } + else + { + return View(); + } } } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs index 8c9b12d..642ba33 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Models/Restaurant.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -8,6 +9,9 @@ namespace OdeToFoodVisualStudio.Models public class Restaurant { public int Id { get; set; } + + [Display(Name="Restaurant Name")] + [Required, MaxLength(80)] public string Name { get; set; } public CuisineType Cuisine { get; set; } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs index c93ba4a..0f96622 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/RestaurantEditModel.cs @@ -1,6 +1,7 @@ using OdeToFoodVisualStudio.Models; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -8,6 +9,7 @@ namespace OdeToFoodVisualStudio.ViewModels { public class RestaurantEditModel { + [Required, MaxLength(80)] public string Name { get; set; } public CuisineType Cuisine { get; set; } } diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Create.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Create.cshtml index 46a452f..016a7c1 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Create.cshtml +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Create.cshtml @@ -6,11 +6,21 @@
- +
+ @* Label will only display when it's a tag pair, NOT a self-closing tag! *@ + + + +
- @* VSRD: No import action for unreferenced CuisineType, have to manually add a @using *@ - - @* The above won't work if the select tag is empty (auto-closed: + + -
\ No newline at end of file + + +
\ No newline at end of file