Data Annotations are used by frameworks such as ASP.NET MVC to enable a model-based validation approach and enforce consistent validation throughout the application, both on client and server side. They were first introduced in ASP.NET MVC 2. In addition to ASP.NET MVC, they can also be used with other technologies such as Entity Framework, either through manual placement of attributes on properties, or automatic generation with T4 templates.
Questions tagged [data-annotations]
3030 questions
18
votes
1 answer
Where are the Entity Framework t4 templates for Data Annotations?
I have been googling this non stop for 2 days now and can't find a single complete, ready to use, fully implemented t4 template that generates DataAnnotations. Do they even exist?
I generate POCOs with the standard t4 templates. The actual…

JK.
- 21,477
- 35
- 135
- 214
18
votes
4 answers
Using DataAnnotations on Windows Forms project
I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same approach for a Forms project but I'm not sure how to go about it.
I have set my attributes but they do not seem to get checked when I click Save.
UPDATE: I have used…

Jon
- 38,814
- 81
- 233
- 382
18
votes
8 answers
MVC: Override default ValidationMessage
In the world of MVC I have this view model...
public class MyViewModel{
[Required]
public string FirstName{ get; set; } }
...and this sort of thing in my view...
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<%=…

ETFairfax
- 3,794
- 8
- 40
- 58
17
votes
3 answers
Where is the whole list of default error messages for DataAnnotations at MVC 3
Yet another MVC localization question...
I'm trying to localize an ASP.Net MVC 3 app using localized Resource files to display texts in the views, as recommended.
The problem is, as usual, when trying to localize the default error messages from data…

Sergi
- 391
- 1
- 4
- 10
17
votes
4 answers
MVC data annotation to compare one property to another?
I've been playing around data annotations in MVC2 and am curious if there is an annotation to compare 2 properties (ie. password, confirm password)?

devlife
- 15,275
- 27
- 77
- 131
17
votes
2 answers
Mark a field "Read Only" with Data Annotations
I am trying to make the ID field read only. It is an Identity field in the DB so the user will not be setting it. However they would like to see it. What am I missing as the below, when assigned to a DataForm still allows that value to be…

Refracted Paladin
- 12,096
- 33
- 123
- 233
16
votes
3 answers
Model Validation to allow only alphabet characters in textbox
How can I annotate my model so I can allow only alphabets like A-Z in my text-box?
I know that I can use regex but can anyone show how to do that on text-box property itself using data annotation.

updev
- 623
- 5
- 14
- 32
16
votes
6 answers
IIS doesn't recognise view model annotations
I have a basic MVC view model with annotations, for example:
[Required(ErrorMessage="Your Name Required")]
[Display(Name = "Your Name")]
[DataType(DataType.Text)]
[MaxLength(120, ErrorMessage = "Must be under 120 characters")] …
user338195
16
votes
2 answers
DataType vs UiHint
I have been using mvc2 for a while now, and when i need to set the template i use the DataType Attribute
[DataType("DropDown")]
public int Field { get; set; }
I see others using UiHint to achieve the same results
…

Andrey
- 1,247
- 11
- 30
16
votes
2 answers
ModelState.IsValid vs IValidateableObject in MVC3
so according to Gu IValidatableObject.Validate() should get called when a controller validates it's model (i.e. before ModelState.IsValid) however simply making the model implement IValidatableObject doesn't seem to work, because Validate(..)…

Master Morality
- 5,837
- 6
- 31
- 43
16
votes
4 answers
@Html.EditorFor DateTime not displaying when set a default value to it
I'd like to set a default value to my model in Controller, But It cannot display in create page.
TestModel code:
public class TestModel
{
[DataType(DataType.DateTime), Required]
[DisplayFormat(DataFormatString = "yyyy/MM/dd",…

Sue Su
- 301
- 1
- 2
- 11
16
votes
1 answer
Using BootstrapValidator with MVC DataAnnotations
I am using DataAnnotations to specify my validation rules and by default, these validation rules are added on the client side for them to be validated by jquery.
I'd like to use BootstrapValidator.js since I like the way the error/success messages…

DotnetDude
- 11,617
- 35
- 100
- 158
16
votes
1 answer
How can I make a DateTime model property required?
I have a model that has a datetime property and I want to make sure that in the view, the form can't be submitted unless that editor for has a value.
employee {
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",…

Abdul Ahmad
- 9,673
- 16
- 64
- 127
16
votes
3 answers
MVC DataAnnotation Accept No Spaces
I'm developing a Login View in MVC5.
I would like to set at the ViewModel level a DataAnnotation to state the the field does NOT accept empty spaces.
Is there a Data Annotation in MVC (something like [NoSpaces] that can be used to NOT allow a string…

SF Developer
- 5,244
- 14
- 60
- 106
16
votes
2 answers
Rendering a using data annotations
I'd like to render a text area for one of the fields in my model.
I've tried applying [DataType(DataType.MultilineText)] to my field, but this doesn't do the trick.
At the moment, I am rendering the text area manually but I'd much prefer to use…

serlingpa
- 12,024
- 24
- 80
- 130