In this article I will explain with an example, how to implement Server Side validation for DropDownList in ASP.Net Core MVC.
The Server Side validation will be performed for DropDownList using Model and Data Annotations in ASP.Net Core MVC.
Model
The Model class consists of following property Gender. The property is decorated with the following Data Annotation attribute for performing validation.
1. Required Data Annotation atribute.
Note: The Data Annotations attribute can be used with the Entity Data Model (EDM), LINQ to SQL, and other data models.
The Required Data Annotation has been specified with a property ErrorMessage with a string value. As the name suggests, this string value will be displayed to the user when the validation fails.
public class PersonModel
{
[Required(ErrorMessage = "Gender is required.")]
public string Gender { get; set; }
}
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method is called when the form is submitted which accepts the PersonModel class object as parameter.
The state of the submitted Model is checked using ModelState.IsValid property.
Then, a check is performed if the Model is valid then the value of the
Gender property is set into a
ViewBag object.
Finally, the View is returned.
Note: ModelState.IsValidproperty is an inbuilt property which verifies two things:
1. Whether the Form values are bound to the Model.
2. All the validations specified inside Model class using Data annotations have been passed.
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(PersonModel person)
{
if (ModelState.IsValid)
{
ViewBag.Gender = person.Gender;
}
return View();
}
}
View
HTML markup
Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Implementing Validation
The Form consists of a HTML INPUT SELECT element i.e. DropDownList with three options, an HTML SPAN and a Submit Button.
The DropDownList has been set with the following Tag Helpers attribute:-
asp-for – The Model property for which validation will be performed. i.e. Gender.
The HTML SPAN has been set with the following Tag Helpers attribute:-
asp-validation-for – Displaying the validation message for the Gender property.
Submitting the Form
When the Submit button is clicked, the Form gets submitted and the Gender value is sent to the Controller.
Finally, a check is performed for Model and if the Model is valid, then the value of the
ViewBag object is displayed.
@model DropDownList_Validation_ServerSide_Core_MVC.Models.PersonModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<table>
<tr>
<td>
<select asp-for="Gender">
<option value="">Please select</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
</td>
<td><span asp-validation-for="Gender" class="error"></span></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
<td></td>
</tr>
</table>
</form>
<hr />
@if (ViewData.ModelState.IsValid)
{
@ViewBag.Gender;
}
</body>
</html>
Screenshot
Downloads