Can we do validation in MVC using data annotations
One of the easy ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which you can be applied on the model properties. For example in the below code snippet we have a simple “customer” class with a property “customercode”.
This”CustomerCode” property is tagged with a “Required” data annotation attribute. In other words if this model is not provided customer code it will not accept the same.
public class Customer
[Required(ErrorMessage="Customer code is required")]
public string CustomerCode
In order to display the validation error message we need to use “ValidateMessageFor” method which belongs to the “Html” helper class.
<% using (Html.BeginForm("PostCustomer", "Home", FormMethod.Post))
<%=Html.TextBoxFor(m => m.CustomerCode)%>
<%=Html.ValidationMessageFor(m => m.CustomerCode)%>
Later in the controller we can chec