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
46
votes
8 answers

Get [DisplayName] attribute of a property in strongly-typed way

Good day! I've such method to get [DisplayName] attribute value of a property (which is attached directly or using [MetadataType] attribute). I use it in rare cases where I need to get [DisplayName] in controller code. public static class…
artvolk
  • 9,448
  • 11
  • 56
  • 85
45
votes
1 answer

What is use of UIHint attribute in MVC

Can anyone please explain me what is the use of UIHint attribute in MVC . Why do we need this. and when and how to use . Thanks
user1030181
  • 1,995
  • 5
  • 26
  • 56
45
votes
5 answers

Custom model validation of dependent properties using Data Annotations

Since now I've used the excellent FluentValidation library to validate my model classes. In web applications I use it in conjunction with the jquery.validate plugin to perform client side validation as well. One drawback is that much of the…
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
43
votes
2 answers

Validator.TryValidateObject Not Validating RangeAttribute

Given the following object, public class Question { [Required] public string QuestionText { get; set; } [Range(1, 5)] public int Difficulty { get; set; } } With the following Validation Code ICollection
James Hughes
  • 6,194
  • 4
  • 31
  • 44
41
votes
4 answers

ASP.NET Core MetaDataType Attribute not working

I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set. But it doesn't do as advertised. What is causing…
Meta
  • 469
  • 1
  • 4
  • 10
40
votes
5 answers

Data Annotations for validation, at least one required field?

If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least…
Boob
  • 1,023
  • 4
  • 13
  • 20
40
votes
4 answers

The field must be a number. How to change this message to another language?

How can I change that messages for all int fields so that instead of saying: The field must be a number in English, it shows: El campo tiene que ser numerico in Spanish. Is there are a way?
Diego_DX
  • 1,051
  • 1
  • 20
  • 33
39
votes
2 answers

Validating DataAnnotations with Validator class

I'm trying to validate a class decorated with data annotation with the Validator class. It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the…
Pablote
  • 4,745
  • 9
  • 39
  • 46
39
votes
3 answers

What parameters does the stringlength attribute errormessage take?

In the MVC4 template one of the data annotation attributes used is stringlength. For example: [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] What parameters {0}, {1}, {2} (more?) are…
Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20
38
votes
2 answers

Entity Framework 4.1 InverseProperty Attribute

Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. Does anyone know any useful resources about the scenarios that this attribute becomes useful? …
Kamyar
  • 18,639
  • 9
  • 97
  • 171
37
votes
8 answers

Why is DisplayFormat DataFormatString not working?

I have a property in my view model as follows: [Editable(false)] [Display(Name = "Date")] [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)] public DateTime MovementDate { get; set; } Yet the markup …
ProfK
  • 49,207
  • 121
  • 399
  • 775
37
votes
5 answers

ASP.NET MVC Validation form with AngularJS

I'm with a project in MVC 4 and AngularJS (+ twitter bootstrap). I usually use in my MVC projects "jQuery.Validate", "DataAnnotations" and "Razor". Then I enable these keys in my web.config to validate properties of model on the client:
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
36
votes
4 answers

What does it mean for a property to be [Required] and nullable?

What does it mean for a property to be [Required] and nullable? (example below) It seems that if it is [Required] it couldn't possibly be null (no value), and if it is able to be null it couldn't possibly be [Required]. [Required] public DateTime?…
Pismotality
  • 2,149
  • 7
  • 24
  • 30
36
votes
7 answers

Validate data using DataAnnotations with WPF & Entity Framework?

Is there any way to validate using DataAnnotations in WPF & Entity Framework?
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
36
votes
4 answers

DataAnnotations StringLength Attribute MVC - without max value

I am using Data Annotations for Model validation in MVC4 and am currently using the StringLengthAttribute however i do NOT want to specify a maximum value (currently set at 50) but DO want to specify a minimum string length value. Is there a way to…
Mr-DC
  • 799
  • 5
  • 14
  • 25