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
23
votes
3 answers

Is there out-of-the box validator for Enum values in DataAnnotations namespace?

C# enum values are not limited to only values listed in it's definition and may store any value of it's base type. If base type is not defined than Int32 or simply int is used. I am developing a WCF serivice which needs to be confident that some…
Mike
  • 2,468
  • 3
  • 25
  • 36
22
votes
5 answers

Opposite of [compare(" ")] data annotation in .net?

What is the opposite/negate of [Compare(" ")] data annotation" in ASP.NET? i.e: two properties must hold different values. public string UserName { get; set; } [Something["UserName"]] public string Password { get; set; }
RollerCosta
  • 5,020
  • 9
  • 53
  • 71
22
votes
2 answers

How to bind view model property with different name

Is there a way to make a reflection for a view model property as an element with different name and id values on the html side. That is the main question of what I want to achieve. So the basic introduction for the question is like: 1- I have a view…
Yusuf Uzun
  • 1,491
  • 1
  • 13
  • 32
22
votes
2 answers

The DataAnnotations [Phone] Attribute

What is the default, valid format of the [Phone] attribute? In the data table, the phone column is navrchar (16) If I enter a phone # like 1112223333, I get "field is not a valid phone number." If I enter 01112223333, I get "The value…
usefulBee
  • 9,250
  • 10
  • 51
  • 89
22
votes
4 answers

Data Annotations with Entity Framework 5.0 (database first)

What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach? This is my partial class created by Entity…
Mithrilhall
  • 1,485
  • 8
  • 33
  • 52
21
votes
3 answers

Conditional data annotation

Is there a way to make a data annotation conditional? I have a table Party where I store both organisations and persons. If I'm adding an organisation I don't want the field surname to be required, but only if I'm adding a person. public class…
Niklas
  • 13,005
  • 23
  • 79
  • 119
21
votes
2 answers

localize data annotations default messages ([Required] [StringLength] etc.)

if I decorate the properties of my ViewModels with attributes like this: public class Vm { [Required] [StringLength(35)] public string Name {get;set;} } I am going to get english validation messages: "this field is required" "The field Name must…
Omu
  • 69,856
  • 92
  • 277
  • 407
21
votes
2 answers

ASP.NET MVC data annotations client side validation with inherited RegularExpressionAttribute

To keep my model validation clean I would like to implement my own validation attributes, like PhoneNumberAttribute and EmailAttribute. Some of these can favorably be be implemented as simple classes that inherit from…
21
votes
5 answers

Is the DataTypeAttribute validation working in MVC2?

As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC v1. For example, public class Model { [DataType("EmailAddress")] public string Email {get; set;} } In the codes above, the Email…
wenqiang
  • 954
  • 1
  • 5
  • 14
21
votes
3 answers

Web API nullable required property requires DataMember attribute

I am receiving the following VM on a Web API Post action public class ViewModel { public string Name { get; set; } [Required] public int? Street { get; set; } } When I make a post I get the following error: Property 'Street' on type…
jorgehmv
  • 3,633
  • 3
  • 25
  • 39
20
votes
3 answers

MVC unobtrusive range validation of dynamic values

I have a value on my model, that must fall within the range of two other values on my model. For example: public class RangeValidationSampleModel { int Value { get; set; } int MinValue { get; set; } int MaxValue { get; set; } } Of…
20
votes
5 answers

DateTime (date and hour) validation with Data Annotation

I have the following code: [DisplayName("58.Date and hour of birth")] [DataType(DataType.DateTime, ErrorMessage = "Please enter a valid date in the format dd/mm/yyyy hh:mm")] [Range(typeof(DateTime), "1/1/2011", "1/1/2016")] …
Marc
  • 2,023
  • 4
  • 16
  • 30
20
votes
4 answers

Validation in Xamarin using DataAnnotation

I am trying to add validations in Xamarin. For that I have used this post as a reference point: Validation using Data Annotation. Following is my Behavior. public class EntryValidationBehavior : Behavior { private Entry…
Safi Mustafa
  • 515
  • 7
  • 22
20
votes
3 answers

Validating an email string in .net using EmailAddressAttribute, but not on an attribute

I want to be able to do this: string email = "some@email.com"; bool isValid = IsValidEmail(email); //returns true ...but use the logic Microsoft has already given us in EmailAddressAttribute. There are hundreds of good and bad implementations of…
Kjensen
  • 12,447
  • 36
  • 109
  • 171
20
votes
3 answers

DataAnnotations - Disallow Numbers, or only allow given strings

Is it possible to use ASP.NET MVC 2's DataAnnotations to only allow characters (no number), or even provide a whitelist of allowed strings? Example?
Alex
  • 75,813
  • 86
  • 255
  • 348