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
35
votes
5 answers

Allow empty strings for fields marked with PhoneAttribute or UrlAttribute

I'm using CodeFirst Entitty framework 5. I have a class representing a user. public class User { [Key] public int UserId { get; set; } [Url] [DataType(DataType.Url)] [Required(AllowEmptyStrings= true)] public string WebSite…
Nu-hin
  • 691
  • 1
  • 7
  • 12
34
votes
3 answers

How can I use the Data Validation Attributes in C# in a non-ASP.net context?

I'd like to use the data validation attributes in a library assembly, so that any consumer of the data can validate it without using a ModelBinder (in a console application, for instance). How can I do it?
Chris McCall
  • 10,317
  • 8
  • 49
  • 80
34
votes
4 answers

Best Practices ViewModel Validation in ASP.NET MVC

I am using DataAnnotations to validate my ViewModel on client side with jquery.validate.unobtrusive and on server side in ASP.NET MVC application. Not so long time ago, I figured out that I can write validation like this: [Required(ErrorMessage =…
34
votes
4 answers

DataAnnotation for Required property

First it works, but today it failed! This is how I define the date property: [Display(Name = "Date")] [Required(ErrorMessage = "Date of Submission is required.")] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode =…
Blaise
  • 21,314
  • 28
  • 108
  • 169
33
votes
4 answers

DataAnnotations dynamically attaching attributes

Apparently it is possible to dynamically attach DataAnnotation attributes to object properties at runtime and as such achieve dynamic validation. Can someone provide code sample on this?
mare
  • 13,033
  • 24
  • 102
  • 191
33
votes
2 answers

DataAnnotation to compare two properties

Is there any way of using data annotations to compare two form field (eg. to confirm an email address) are the same, before allowing the form to be posted? eg. can the regular expression data annotation use the match function to reference another…
Mark
  • 7,778
  • 24
  • 89
  • 147
32
votes
3 answers

Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

I've been using the new ASP.Net MVC 3 RemoteAttribute to send a remote call to an action method that had a single parameter. Now I want to pass in a second parameter using the AdditionalFields property: [Remote("IsEmailAvailable", "Users",…
beaudetious
  • 2,354
  • 3
  • 36
  • 60
32
votes
3 answers

mvc4 data annotation compare two dates

I have these two fields in my model: [Required(ErrorMessage="The start date is required")] [Display(Name="Start Date")] [DisplayFormat(DataFormatString = "{0,d}")] public DateTime startDate { get; set; } [Required(ErrorMessage="The end date is…
user2208349
  • 7,429
  • 5
  • 26
  • 35
32
votes
6 answers

mvc [DataType(DataType.EmailAddress) no validation

I'm using this code on an email field: [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [DataType(DataType.EmailAddress)] does not work (validation does not occur no at…
GibboK
  • 71,848
  • 143
  • 435
  • 658
31
votes
4 answers

Entity Framework Code First : how to annotate a foreign key for a "Default" value?

I have 2 classes: Client and Survey. Each Client can have many surveys - but only one default survey. I have defined the classes like this: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set;…
Colin
  • 22,328
  • 17
  • 103
  • 197
30
votes
1 answer

How to change default validation error message in ASP.NET MVC?

Say I have this property in my model: [DisplayName("test")] [Required(ErrorMessage = "required")] public DateTime? SomeDate { get; set; } when you type in "asdf" in Html.TextBoxFor(model => model.SomeDate), you get the validation error message "The…
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
30
votes
2 answers

ASP.NET-MVC 2 DataAnnotations StringLength

Can I use the MVC 2 DataAnnotations to specify a minimum length for a string field? Has anyone done this or have they created custom attributes and if so do you mind sharing the source?
griegs
  • 22,624
  • 33
  • 128
  • 205
30
votes
5 answers

MVC Validation Lower/Higher than other value

How is the best way to validate a model in MVC.Net where I want to accept a minimum/maximum. Not individual min/max values for a field. But separate fields for a user to specify a minimum/maximum. public class FinanceModel{ public int MinimumCost…
Ben Ford
  • 1,354
  • 2
  • 14
  • 35
28
votes
2 answers

Is it possible to use DataAnnotations with Interfaces?

I want to use DataAnnotations to validate classes that implements some interfaces, and so I'm adding validation attributes to the interface, like this: public interface IUser { [Required] string Name { get; set; } [Display(Name = "Email…
Bruno
  • 4,337
  • 12
  • 42
  • 55
28
votes
4 answers

MVC Razor Validation Errors showing on page load when no data has been posted

I'm messing around with data annotations. When I click on a link to go to a page, the validation messages are being displayed, but I would like to have the validation messages not show unless data has been posted. View: @Html.TextBoxFor(m =>…
allencoded
  • 7,015
  • 17
  • 72
  • 126