Model validation is a way where we add attribute (derives from ValidationAttribute) to a property for validate it value. To go ahead first go through the below code snippets of Model and Controller.
Customer.cs
CustomerController.cs
Above code snippets working fine but there is not any validation for model and we will go ahead by referring above snippets and changes required for model validation.
Take an example below are the list of rules for Customer model
In your MVC project references there is already library referenced 'System.ComponentModel.DataAnnotations' if not you can download the package from nuget.
Now below are implementing the validation for First Name property in Customer model.
Requirement: First Name field not be empty.
Step 1: Add the namespace 'using System.ComponentModel.DataAnnotations;' in Customer Model class.
Step 2: Add the attribute '[Required]' on FirstName property.
Step 3: Add the if statement ModelState.IsValid in Action method and based on the response go ahead.
that's it :)
Customer.cs
CustomerController.cs
You can implement the remaining validation like above and finally our class look like below.
Hope this article help you to understand Model validation in MVC or Web API.
You can refer this link for more details https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx
Customer.cs
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string EmailId { get; set; }
public string TelephoneNumber { get; set; }
public string MobileNumber { get; set; }
}
CustomerController.cs
public class CustomerController : Controller { [HttpPost] public ActionResult Create(Customer customer) {
// TODO: Add insert logic here
} }
Above code snippets working fine but there is not any validation for model and we will go ahead by referring above snippets and changes required for model validation.
Take an example below are the list of rules for Customer model
- First Name field should not be empty.
- City field should be string and max length is 40.
- Email Id field should be valid email address and can be empty.
- Telephone Number field should be numeric and can be empty.
- Mobile Number field should be numeric and can NOT be empty IF telephone field empty.
In your MVC project references there is already library referenced 'System.ComponentModel.DataAnnotations' if not you can download the package from nuget.
Now below are implementing the validation for First Name property in Customer model.
Requirement: First Name field not be empty.
Step 1: Add the namespace 'using System.ComponentModel.DataAnnotations;' in Customer Model class.
Step 2: Add the attribute '[Required]' on FirstName property.
Step 3: Add the if statement ModelState.IsValid in Action method and based on the response go ahead.
that's it :)
Customer.cs
[Required]
public string FirstName { get; set; }
CustomerController.cs
[HttpPost]
public string Create(Customer customer)
{
if (ModelState.IsValid)
{
// TODO: Add insert logic here
}
else
{
return "error: invalid data";
}
}
You can implement the remaining validation like above and finally our class look like below.
public class Customer
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
[MaxLength(40)]
public string City { get; set; }
[EmailAddress]
public string EmailId { get; set; }
[DataType(DataType.PhoneNumber)]
public string TelephoneNumber { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
public string MobileNumber { get; set; }
}
Hope this article help you to understand Model validation in MVC or Web API.
You can refer this link for more details https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx