Questions tagged [data-annotations]

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.

3030 questions
210
votes
10 answers

How to specify a min but no max decimal using the range data annotation attribute?

I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value. Here's what I have so far...I'm not sure what the correct way to do this is. [Range(typeof(decimal), "0", "??"] public decimal Price…
user169867
  • 5,732
  • 10
  • 39
  • 56
162
votes
2 answers

Fluent Validation vs. Data Annotations

What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have similar objects, all the way to their object names. Is one related to another? What are their differences? In what way…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
153
votes
4 answers

displayname attribute vs display attribute

What is difference between DisplayName attribute and Display attribute in ASP.NET MVC?
147
votes
6 answers

Entity Framework code first unique column

I am using Entity Framework 4.3 and using Code Fist. I have a class public class User { public int UserId{get;set;} public string UserName{get;set;} } How do I tell Entity Framework that UserName has to be unique when creating database…
cpoDesign
  • 8,953
  • 13
  • 62
  • 106
146
votes
17 answers

Disable Required validation attribute under certain circumstances

I was wondering if it is possible to disable the Required validation attribute in certain controller actions. I am wondering this because on one of my edit forms I do not require the user to enter values for fields that they have already specified…
Alex Hope O'Connor
  • 9,354
  • 22
  • 69
  • 112
128
votes
9 answers

Int or Number DataType for DataAnnotation validation attribute

On my MVC3 project, I store score prediction for football/soccer/hockey/... sport game. So one of properties of my prediction class looks like this: [Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")] [StringLength(2, ErrorMessage = "Max 2…
Antonin Jelinek
  • 2,277
  • 2
  • 20
  • 25
125
votes
18 answers

Why can't I reference System.ComponentModel.DataAnnotations?

I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following: using System.ComponentModel.DataAnnotations; However, I get the error The type or namespace name 'DataAnnotations' does not exist in…
DaveDev
  • 41,155
  • 72
  • 223
  • 385
115
votes
11 answers

Assign format of DateTime with data annotations?

I have this attribute in my view model: [DataType(DataType.DateTime)] public DateTime? StartDate { get; set; } If I want to display the date, or populate a textbox with the date, I have these: <%: Model.StartDate %> <%: Html.TextBoxFor(m =>…
Steven
  • 18,761
  • 70
  • 194
  • 296
112
votes
6 answers

ASP.NET MVC: Custom Validation by DataAnnotation

I have a Model with 4 properties which are of type string. I know you can validate the length of a single property by using the StringLength annotation. However I want to validate the length of the 4 properties combined. What is the MVC way to do…
Danny van der Kraan
  • 5,344
  • 6
  • 31
  • 41
96
votes
12 answers

Conditionally required property using data annotations

I have a class like this: public class Document { public int DocumentType{get;set;} [Required] public string Name{get;set;} [Required] public string Name2{get;set;} } Now if I put a [Required] data annotation on the Name and Name2…
brtb
  • 2,201
  • 6
  • 34
  • 53
93
votes
17 answers

MVC Model require true

Is there a way through data annotations to require that a boolean property be set to true? public class MyAwesomeObj{ public bool ThisMustBeTrue{get;set;} }
Marty Trenouth
  • 3,712
  • 6
  • 34
  • 43
85
votes
5 answers

Unit Testing ASP.NET DataAnnotations validation

I am using DataAnnotations for my model validation i.e. [Required(ErrorMessage="Please enter a name")] public string Name { get; set; } In my controller, I am checking the value of ModelState. This is correctly returning false for invalid model…
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
79
votes
6 answers

DataAnnotations: Recursively validating an entire object graph

I have an object graph sprinkled with DataAnnotation attributes, where some properties of objects are classes which themselves have validation attributes, and so on. In the following scenario: public class Employee { [Required] public string…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
77
votes
5 answers

Required attribute for an integer value

I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this attribute is working only for string properties. When no Id is set, Id has value 0 and the model is valid. How can I enforce that if no value for a int…
user256034
  • 4,289
  • 5
  • 37
  • 44
76
votes
1 answer

Asp.Net Mvc Hidden Field from Data Annotations

I thought this would be a quick search on google but maybe I'm missing something. Is there a way, using Data Annotations, to set a ViewModel property to create a HiddenInput when the markup get rendered? The only annotations I've found were to hide…
Justin Soliz
  • 2,781
  • 3
  • 25
  • 33
1
2 3
99 100