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
27
votes
2 answers

how to put DisplayName on ErrorMessage format

I have something like this: [DisplayName("First Name")] [Required(ErrorMessage="{0} is required.")] [StringLength(50, MinimumLength = 10, ErrorMessage="{0}'s length should be between {2} and {1}.")] public string Name { get; set;…
goenning
  • 6,514
  • 1
  • 35
  • 42
27
votes
1 answer

How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

I'm building a MVC web application with C#. Since the site will be multilingual, I've implemented my own ResourceManager. This class is responsible for fetching the required resource strings from a database/cache depending on the currents thread…
Mats
  • 14,902
  • 33
  • 78
  • 110
27
votes
5 answers

ASP.NET MVC: Is Data Annotation Validation Enough?

I'm using the Data Annotation validation extensively in ASP.NET MVC 2. This new feature has been a huge time saver, as I'm now able to define both client-side validation and server-side validation in one place. However, while I was doing some…
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
27
votes
3 answers

Greater Than or Equal To Today Date validation annotation in MVC3

Has anyone seen an MVC3 data annotation for Date validation that requires a single selected date to be equal to or greater than current date? If there's already a third party add on that's cool too. I'm already using the DataAnnotationsExtensions…
CD Smith
  • 6,597
  • 7
  • 40
  • 66
26
votes
5 answers

Annotating properties on a model with default values

I created an EF4.1 code-first model (may or may not be important), and I'm trying to get default values for my Create scaffold template. My model looks like: class Person { [DefaultValue (18)] public int Age { get; set; } } And then my…
Bryan Boettcher
  • 4,412
  • 1
  • 28
  • 49
25
votes
8 answers

How do I invoke a validation attribute for testing?

I am using the RegularExpressionAttribute from DataAnnotations for validation and would like to test my regex. Is there a way to invoke the attribute directly in a unit test? I would like to be able to do something similar to this: public class…
CobraGeek
  • 543
  • 1
  • 5
  • 10
25
votes
4 answers

Error messages for model validation using data annotations

Given the following classes: using System.ComponentModel.DataAnnotations; public class Book{ public Contact PrimaryContact{get; set;} public Contact SecondaryContact{get; set;} [Required(ErrorMessage="Book name is required")] public…
IaCoder
  • 12,300
  • 11
  • 37
  • 45
25
votes
1 answer

What is the proper data annotation to format my decimal property?

I have a POCO with a decimal property called SizeUS. I would like to use data annotations to format the display of the decimal in a view. My SizeUS property is only displaying 2 decimal places in my view and I want it to display 4 decimal places. …
Bill Greer
  • 3,046
  • 9
  • 49
  • 80
24
votes
9 answers

Validation of Guid

I have a strongly-typed view which has a DropDownListFor attribute on it. Each item in the dropdown list is represented by a GUID. What I'm after is a way to validate if a user selects an item from the dropdown list. At present i don't see anyway…
Hesky
  • 787
  • 3
  • 8
  • 14
24
votes
4 answers

How do Data Annotations work?

I use Data Annotations in my ASP.NET MVC 3 project to validate the model. These are extremely convenient but currently they are magic to me. I read that data annotations do not throw exceptions. How then does MVC know to add validation errors to the…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
24
votes
1 answer

Understanding ValidationContext in DataAnnotations

I want to utilize Validator.TryValidateValue() but don't understand the mechanics. Say, i have the following: public class User { [Required(AllowEmptyStrings = false)] [StringLength(6)] public string Name { get; set; } } and the…
UserControl
  • 14,766
  • 20
  • 100
  • 187
24
votes
6 answers

MVC 5 Remote Validation

I need to validate an input field value from user before the form is submitted. I have created an action in my custom controller and decorated the field with it: action name: CheckValue controller name: Validate [Remote("CheckValue",…
user2818430
  • 5,853
  • 21
  • 82
  • 148
23
votes
1 answer

Password DataAnnotation in ASP.NET MVC 3

Possible Duplicate: Password validation (regex?) I am working on asp.net MVC 3 application and I have applied [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] …
Asif Hameed
  • 1,353
  • 9
  • 25
  • 46
23
votes
2 answers

Foreign keys in entity framework 4.1

I am working on Entity Framework 4.1 and using data annotations for foreign keys. I want to know how can we define one to many relationship between product and categories. I want to map category. categoryId with product.cid public class Category { …
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
23
votes
1 answer

Using ASP.Net MVC Data Annotation outside of MVC

i was wondering if there is a way to use ASP.Net's Data annotation without the MVC site. My example is that i have a class that once created needs to be validated, or will throw an error. I like the data annotations method, instead of a bunch of if…
Doug
  • 6,460
  • 5
  • 59
  • 83