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
75
votes
7 answers
How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?
In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor (textarea)? I am using [UIHint("MultilineText")], but can't find any documentation on how to add attributes for the text area.
Desired HTML:
73
votes
16 answers
"The Id field is required" validation message on Create; Id not set to [Required]
This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2.
The POCO has the following properties:
public int Id {get;set;}
[Required]
public string Message {get; set}
On the creation of the entity, the Id is…

Dan
- 12,808
- 7
- 45
- 54
70
votes
4 answers
Data Annotation to validate confirm password
My User model has these data annotations to validate input fields:
[Required(ErrorMessage = "Username is required")]
[StringLength(16, ErrorMessage = "Must be between 3 and 16 characters", MinimumLength = 3)]
public string Username { get; set;…

James Dawson
- 5,309
- 20
- 72
- 126
69
votes
7 answers
How to retrieve Data Annotations from code? (programmatically)
I'm using System.ComponentModel.DataAnnotations to provide validation for my Entity Framework 4.1 project.
For example:
public class Player
{
[Required]
[MaxLength(30)]
[Display(Name = "Player Name")]
public string PlayerName { get;…
user356178
66
votes
15 answers
Entity Framework Code First Fluent Api: Adding Indexes to columns
I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects.
As an example lets say we have this employee class:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeCode { get; set; }
…

Jim Wolff
- 5,052
- 5
- 34
- 44
66
votes
3 answers
How can I tell the Data Annotations validator to also validate complex child properties?
Can I automatically validate complex child objects when validating a parent object and include the results in the populated ICollection?
If I run the following code:
using System;
using System.Collections.Generic;
using…

GWB
- 2,575
- 2
- 23
- 28
64
votes
3 answers
How to create Custom Data Annotation Validators
Wanting to create custom data annotation validation. Are there any useful guides / samples on how to create them?
Firstly:
StringLength with minimum and maximum length. I'm aware .NET 4 can do this, but want to do the same in .NET 3.5, if possible…

SamWM
- 5,196
- 12
- 56
- 85
60
votes
10 answers
The specified value does not conform to the required format yyyy-MM-dd
I have a .Net MVC 5 application that is using Data Annotations, Entity-Framework Jquery 2.1.3 and Jquery UI 1.11.4.
When I render an edit form with an input of type date using the UK format "dd/MM/YYYY"; the following error message appears when…

Andy Clark
- 3,363
- 7
- 27
- 42
54
votes
5 answers
How to add "required" attribute to mvc razor viewmodel text input editor
I have the following MVC 5 Razor HTML helper:
@Html.TextBoxFor(m => m.ShortName,
new { @class = "form-control", @placeholder = "short name"})
I need this field to be required (i.e. have a red outline when user navigates out without putting a…

Eugene Goldberg
- 14,286
- 20
- 94
- 167
54
votes
7 answers
Data Annotation Ranges of Dates
Is it possible to use [Range] annotation for dates?
something like
[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())]

Davy
- 1,043
- 2
- 12
- 21
53
votes
12 answers
How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)
I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated.
I have a simple model:
public class Article
{
public int…

Mark Bell
- 28,985
- 26
- 118
- 145
52
votes
12 answers
How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?
I've got a view model like this:
public class SignUpViewModel
{
[Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")]
[DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")]
public bool AgreesWithTerms { get; set;…

asp_net
- 3,567
- 3
- 31
- 58
51
votes
9 answers
DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view
The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.
Model:
[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$",…

Prasad
- 58,881
- 64
- 151
- 199
50
votes
6 answers
Using DataAnnotations to compare two model properties
How would I go about writing a custom ValidationAttribute that compares two fields? This is the common "enter password", "confirm password" scenario. I need to be sure the two fields are equal and to keep things consistent, I want to implement the…

Scott
- 13,735
- 20
- 94
- 152
49
votes
3 answers
ASP.Net MVC DisplayFormat
In my model I have the following DataAnnotations on one of my properties
[Required(ErrorMessage = "*")]
[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }
The required annotation works…

Gavin
- 17,053
- 19
- 64
- 110