Skip to content

Commit

Permalink
Adding model validation with data annotations in models, checking for…
Browse files Browse the repository at this point in the history
… valid model state in controller, and adding validation tag helpers in view
  • Loading branch information
gorohoroh committed Jul 5, 2018
1 parent a2dc138 commit 41e4d77
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
16 changes: 11 additions & 5 deletions OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
5 changes: 5 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Models/Restaurant.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
Expand Down
15 changes: 12 additions & 3 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
<h1>Create</h1>

<form method="post">
<input asp-for="Name"/>
<div>
<label asp-for="Name"></label>
<input asp-for="Name"/>
<span asp-validation-for="Name"></span>
</div>

@*VSRD: Rider's code analysis bug in the asp-items expression below: RSRP-469518 *@
<select asp-for="Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
<div>
<label asp-for="Cuisine"></label>
<select asp-for="Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
<span asp-validation-for="Cuisine"></span>
</div>

<input type="submit" name="save" value="Save"/>
</form>

<div asp-validation-summary="All"></div>
<div>

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,23 @@ 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;

newRestaurant = _restaurantData.Add(newRestaurant);

return RedirectToAction(nameof(Details), new { id=newRestaurant.Id });

}
else
{
return View();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -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; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using OdeToFoodVisualStudio.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace OdeToFoodVisualStudio.ViewModels
{
public class RestaurantEditModel
{
[Required, MaxLength(80)]
public string Name { get; set; }
public CuisineType Cuisine { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@

<form method="post">

<input asp-for="Name"/>
<div>
@* Label will only display when it's a tag pair, NOT a self-closing tag! *@
<label asp-for="Name"></label>
<input asp-for="Name" />
<span asp-validation-for="Name"></span>
</div>

@* VSRD: No import action for unreferenced CuisineType, have to manually add a @using *@
<select asp-for="Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
@* The above won't work if the select tag is empty (auto-closed: <select/>) *@
@* VSRD: Visual Studio has "Wrap with div" action but it's not reformatting the resulting markup, and as Extend Selection doesn't work in Razor, I have to select manually, then invoke Ctrl+K,F to reformat selection *@
<div>
<label asp-for="Cuisine"></label>
<select asp-for="Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
<span asp-validation-for="Cuisine"></span>
</div>

<input type="submit" name="save" value="Save" />
</form>
</form>

<div asp-validation-summary="All"></div>

0 comments on commit 41e4d77

Please sign in to comment.