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.
In order to display the validation error message we need to use “ValidateMessageFor” method which belongs to the “Html” helper class.
Later in the controller we can check if the model is proper or not by using “ModelState.IsValid” property and accordingly we can take actions.
Below is a simple view of how the error message is displayed on the view.
Above is the ASP.NET MVC interview questions & also see the following video on ASP.NET Masterpages: -
write a comment …