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
20
votes
6 answers

Why can't I use resources as ErrorMessage with DataAnnotations?

Why can't I do like this? [Required(ErrorMessage = "*")] [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)] public string Username { get; set; } What is the error message telling me? An attribute…
Johan Alkstål
  • 5,225
  • 9
  • 36
  • 48
20
votes
3 answers

Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels: public class ExamplePersonViewModel { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType =…
Lance McNearney
  • 9,410
  • 4
  • 49
  • 55
20
votes
6 answers

ASP.NET MVC ValidateInput(false) stops working with xVal and [RegularExpression] DataAnnotation

I would like to intercept the "<" character in the form field by a regex validator. I will describe the problem in 3 steps: Step 1: When I try to submit a form with a field containing the "<" character, I get the "Potentially dangerous request..." -…
Alex42
  • 562
  • 1
  • 5
  • 12
19
votes
4 answers

How can I set a RegularExpression data annotation's regular expression argument at runtime?

We manage several ASP.NET MVC client web sites, which all use a data annotation like the following to validate customer email addresses (I haven't included the regex here, for readability): [Required(ErrorMessage="Email is…
Mark Bell
  • 28,985
  • 26
  • 118
  • 145
19
votes
7 answers

ASP.NET MVC datetime culture issue when passing value back to controller

How can i tell my controller/model what kind of culture it should expect for parsing a datetime? I was using some of this post to implement jquery datepicker into my mvc application. When i submit the date it gets "lost in translation" i'm not using…
19
votes
1 answer

Get DisplayAttribute attribute from PropertyInfo

class SomeModel { [Display(Name = "Quantity Required")] public int Qty { get; set; } [Display(Name = "Cost per Item")] public int Cost { get; set; } } I'm trying to map the model into a list of { PropertyName, DisplayName } pairs,…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
19
votes
7 answers

ASP.NET MVC displaying date without time

I have my model field decorated in the following way: [DataType(DataType.Date)] [Display(Name = "Date of birth")] public string DateOfBirth { get; set; } When I want to display the value in the view using the following code: <%: Html.DisplayFor(m…
GUZ
  • 627
  • 1
  • 9
  • 18
19
votes
3 answers

Default resource for data annotations in ASP.NET MVC

There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name {…
19
votes
5 answers

Client form validation not working with modal dialog in MVC

I am changing a create form to become a modal dialog and jquery Unobtrusive validation stops working and don't know how to fix it. Index.cshtml has a link to trigger a modal dialog. Create @section…
19
votes
2 answers

MVC data annotations range validation not working properly

I have a RangeValidator on a property in my model to only allow Integers that are between 0 and 100. I have a partial view that displays a form to update the property via a jQuery UI dialog. I have looked at the source and can confirm that the…
18
votes
1 answer

ASP.Net MVC 3 ViewModel Data Annotations

I am developing an ASP.Net MVC 3 Web application with Entity Framework 4.1 and I am getting a bit confused with regards using Data Annotations for form validation. I always return a ViewModel to a View as opposed to passing the actual object as I…
tcode
  • 5,055
  • 19
  • 65
  • 124
18
votes
2 answers

Compare (password) attribute

I'd like to create a view model for a new user using the code below. The "User" class contains just the two properties (simplified for now) that I will persist to the database; the view model adds a "compare password" field, which is only used in…
Kras
  • 623
  • 2
  • 9
  • 13
18
votes
3 answers

ASP.NET MVC 3 Data Annotation: Add validation dynamically

I'm new with data annotation. I'd like to know if it possible (and how) to add some validation dynamically. It is very extensive to explain why, but I've a ViewModel that receives and object when created. In that object I must check for some…
Diego
  • 16,436
  • 26
  • 84
  • 136
18
votes
3 answers

Required Data Annotation is not being translated

We are facing with an strange error with localization of Required attribute. We have the following code: public class AnswersGroupViewModel { public int IDAnswerGroup { get; set; } public int IDEvaluator { get; set; } …
rubenfa
  • 831
  • 1
  • 7
  • 23
18
votes
3 answers

Range annotation between nothing and 100?

I have a [Range] annotation that looks like this: [Range(0, 100)] public int AvailabilityGoal { get; set; } My webpage looks like this: <%=Html.TextBoxFor(u => u.Group.AvailabilityGoal)%> It works as it should, I can only enter values between 0…
Bobby
  • 2,074
  • 3
  • 15
  • 14