MVC3 Interview Questions - Data Annotations
Q :- What are Data Annotations ? Why it is used ?
Ans : Data Annotations are attributes you find in System.ComponentModel.DataAnnotations namespace. The attributes provide server-side validation and the framework also support client-side validation when you use one of the attributes on a model property. Data Annotations are used to validate forms and user input when the form is posted by the user.
Q :- What are the different Data Annotation attributes ?
Ans : Following are the mostly used Data Annotation attributes :
Ans : Data Annotations are attributes you find in System.ComponentModel.DataAnnotations namespace. The attributes provide server-side validation and the framework also support client-side validation when you use one of the attributes on a model property. Data Annotations are used to validate forms and user input when the form is posted by the user.
Q :- What are the different Data Annotation attributes ?
Ans : Following are the mostly used Data Annotation attributes :
- Required - This attribute makes the property of model mandatory for user.
- StringLength - The StringLength attribute specifies the maximum length of string that is acceptable.
- RegularExpression - Regular expressions are an efficient and terse means to enforce the shape and contents of a string value.
- Range - The Range attribute specified the minimum and maximum constraints for a numerical value.
- Compare - The compare attribute ensures two properties on a model object have the same value.
- Display - The Display attribute sets the friendly name for a model property.
- ScaffoldColumn - The scaffoldColumn attribute hides a property from HTML helper such as EditorForModel and DisplayForModel.
- MaxLength - MaxLength Attribute ensures the length of the string entered is according to the specified value.
- DataType - The DataType attribute enables you to provide the run time with information about the specific purpose of a property.
- UIHint - The UIHint attribute as the name itself suggest gives the Asp.net MVC run time the name of a template to use when rendering output with the templated helpers like (DisplayFor and EditorFor).
- HiddenInput - The property that has HiddenInput defined on it will be rendered as input type hidden on form.
Q :- How to validate string length using Data Annotations ?
Ans : We use StringLength Data Annotation attribute to validate string lenth entered by the user. The StringLength attribute accepts a parameter which is the maximum length of string acceptable.
eg :
In the above image, we have applied StringLentgh Data Annotation attribute to FirstName property of model. We have supplied 10 as length. When the string length entered by user is more than 10, the validations fails and error message is shown to the user.
For more info visit below link :
Q :- How to validate email address using Data Annotation ?
Ans : We use RegularExpression Data Annotation attribute to validate email addresses. This attribute accepts a pattern which is validated against the email provided by the user. If the pattern and email matches the validation passes, other wise error message shown to the user.
eg :
In the above example, we have define a pattern for Email property of model. This pattern is used to validate the email entered by the user.
For more info visit below link :
Q :- How to validate number range using Data Annotation ?
Ans : The Range attribute is use to validate the number range. This attribute accepts minimum and maximum range values. This attribute is use to validate properties like AGE.
eg :
In the above example, we have specified 20 and 40 as minimum and maximum values. The validation fails if the value entered by user is outside the above specified range.
For more info visit below link :
Q :- What is the difference between Range and Stringlength Data Annotation attribute ?
Ans : There is a major difference between the two even thought both validates the numeric measure.
StringLength validates the count of the string entered wherease the Range attribute validates the range i.e. whether the user input is between given range.
StringLength attribute is use to validate properties like Password or Firstname where we don't want user to specify the value more that particular length. Range attribute is used to validate property like Age where we want to user to specify age between given range.
Q :- What is the use of UIHint Data Annotation attribute ?
Ans : The UIHint attribute gives the Asp.net run time, name of the template to use while rendering control using the template helpers like (DisplayFor, EditorFor).
The UIHint attribute accepts the name of the Editor Template to render control for model property.
eg:
When the form or page is rendered, the editor template named Gender is used to render controls or HTML for Gender property.
For more info visit below link :
Q :- How to show error message on UI when Data Annotation validation fails ?
Ans :
1. We use ValidationSummary helper to show the error messages on UI.
For more info visit below link :
When we use HTML helper like EditorForModel to render the form, then the error messages are shown alongside the controls without using ValidationSummary helper.
For more info visit below link :
Q :- How to show multi Language error messages with Data Annotation ?
Ans : We can use the attributes provided by each DataAnnotation attribute. These attributes are :
ErrorMessageResourceType and ErrorMessageResourceName.
eg :
In the above example, we are picking the error message from resource file. The above excepts a resource file with name Multi with key MaxLength_ErrorMessage holding the error message. When validation fails, the system picks error message from resource file.
Q :- What is Required attribute ? Why it is important ?
Ans : The Required attribute is the most basic and important Data Annotation attribute. This attribute as the name suggests makes the property of model mandatory for user to answer. When the value is not provided the error message is shown to the user.
eg :
In the above example, we have applied Required Data Annotation attribute to the FirstName property. The FirstName property is mandatory for user to answer.
For more info visit below link :
Q :- How to use Data Annotation attribute with model ?
Ans : First we need to include System.ComponentModel.DataAnnotations namespace.
We can apply Data Annotation attribute to each property in the model class.
eg :
Q :- What is the significance of ModelState while using DataAnnotation ?
Ans : We check the validity of ModelState to determine whether the Data Annotation validation is failed or not.
ModelState becomes invalid of the Data Annotation validation fails.
eg :
In the above example, we have checked if the ModelState is valid or not. If it is valid then we are rendering the view without passing model, but when the ModelState is invalid we are passing model object with errors to the view.
Q :- What are Custom Data Annotation attributes ?
Ans : We can create our own Custom Data Annotation attributes to validate as per our needs. We need to inherit ValidationAttribute class and override IsValid() method to create simple validation attribute.
eg :
In the below link we have created a custom attribute to validate email address :
Q :- What is the use of ScaffoldColumn Data Annotation attribute ?
Ans : The ScaffoldColumn attribute hides the property from HTML hepler like EditorForModel and DisplayForModel.
This attribute accepts a boolean parameter. With this attribute in place the EditorForModel helper will not render the control for property for which this attribute is set to false.
eg :
We have applied the attribute to Age property of model. When the form is rendered the Age property will be ignored by the helper.
In the above view we have used EditorForModel helper to render the form for model.
As mentioned earlier the Age property is not rendered on the form.
For more infor visit below link :
0 comments:
Post a Comment