dataannotations

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 { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResour...

C# RegularExpression Attribute constructor called once

I am using data annotations to validate an emailaddress. To show an error message when the emailaddress is not valid, I use a RESX file called ErrorMessages. My code is like this: public class EmailAdressAttribute : RegularExpressionAttribute { public EmailAdressAttribute() : base(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0...

How to use DataType.Custom in ASP.NET MVC 2?

Can anyone tell me how to use DataType.Custom in ASP.NET MVC 2? ...

What is the 'best way' or 'best practise' for communicating errors to the View from a service layer in MVC2?

I had issued with using a ModelStateWrapper in MVC 1 as described here: http://www.asp.net/mvc/tutorials/validating-with-a-service-layer--cs As much as I was able to test my application it still felt like there was a dependency/circular reference with the ModelState, the controller and the service layer. With the new DataAnnotation in M...

MVC DataAnnotations - Require at least one field in a group to be filled

Hi, How can I use DataAnnotations to validate that at least one of these fields are filled in? public string Name { get; set; } public string State { get; set;} public string Zip { get; set;} ...

How to get a lambda expression from a PropertyDescriptor

I have some code that enumerates over an object and records any errors it has based on its ValidationAttribute(s). As it finds them I wish to create a collection of a custom class named RuleViolations. The RuleViolation class looks like this: public string Message { get; set; } public LambdaExpression Property { get; set; } ...

DataAnnotations [Required] Attribute doesn't cause Exception to be thrown

Hi I have an Asp.NET MVC application in which I use data annotations to add validation to some fields: [Required] [DisplayName("Course Name")] string Name { get; set; } However this doesn't seem to work as I expected. Basically If the page contains any other errors that I manually check for and throw a new RuleViolation()...

How to use non-static enum values with the DefaultValue data annotation?

public enum ProductQuantityType { Weight = 1, Volume = 2, Custom = 0 } This fails [MetadataType(typeof(ProductMetaData))] public partial class Product { public class ProductMetaData { [DefaultValue(ProductQuantityType.Weight)] public object QuantityType { get; set; } } } Error: An object reference...

How much and which data annotations to use?

Hi there. I read many articles and posts about data annotations. But most of them just simple 'How to use the xxx attribute' articles. But what i did not find is any article on which annotations should be used and why or why not. I mean, its clear that its good to use things like StringLength, Range or Required attributes and it's clear...

When implementing a DataAnnotations validation attribute, should I call base.IsValid()?

I'm creating a DataAnnotations validation attribute for matching emails using the same pattern as jQuery (yes, it must have been done before, but I can't find it...) and I'm not sure on exactly what I'm supposed to override and whether methods on the base classes should be called or not. Currently I have this implemnetation: public clas...

MVC Validation using Data Annotations - Model classes or View Model classes?

Is it best practice to put data validation annotations in the Model or View Model? What are the advantages/disadvantages of one approach over the other? Curious to see where everyone is putting their validation, I am currently doing it in the model project. However I have seen a few people say this is not best practice. ...

MVC Validation using Data Annotations - Scenarios where it doesn't work, Alternatives?

So I have been using data annotations for my validation in an MVC project and they seem to work in most scenarios. Here are two examples in my current project where they don't seem to fit and I am unsure of the best place to put the validation. 1) I have a Join League page that contains a form where the logged in user enters their t...

A strongly typed, localizable DisplayNameAttribute

I'm trying to write a strongly typed, localizable DisplayNameAttribute, but I can't get it to even compile. What I'd like to do on my viewmodel properties is something like [LocalizedDisplayName<HomeResources>(r => r.WelcomeMessage)] which would basically do the same thing as [DisplayName("Welcome to my site!")] except the message ...

How to specify a min but no max decimal using the range data annotation attribute?

I would like to specify that a decimal field for a price must be >= 0 but I don't really want to impose a max value. Here's what I have so far...I'm not sure what the correct way to do this is. [Range(typeof(decimal), "0", "??"] public decimal Price { get; set; } ...

MVC client validation causing false errors

I'm hoping someone has run into something similar. A text input with text is triggering the client-side [Required] error message. I have the following included in the master: <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> <script src...

Custom validation attribute with multiple instances problem

I'm using tha namespace System.ComponentModel.DataAnnotations in C# 4 to implement my own validation attribute and it looks like this [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public sealed class MyCustomValidator : ValidationAttribute { private String Property1 { get; set; } private String Property2 { get; ...

Is there a way to remove attributes from an inherited property?

Is it possible to remove attributes from inherited properties? I thought that by using the new keyword I could do so... public class Person { [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } } public class Employee : Person { [Required] public string J...

Asp.net mvc 2 custom view model: where would the validation attributes go?

I've been chugging along OK with the use of data annotations made on buddy classes so far. When it comes to a more complex view that requires a custom view model--one that includes a few select lists, for example...would I need to then transfer my validation attributes to the view model class? I was planning to pass the full custom vie...

Server/Client input validation for asp.net form using attribute / dataannotation

I was playing with asp.net mvc 2's DataAnnotation validation. It hookup to the client side validation very nicely. I really like that I can just define one sets of rules and be able to user it at both client and server side. I am wondering if it is possible to do it with asp.net form. The following code snippet shows how it is done, an...

Bypass Data Annotations validation on ASP.NET MVC 2

I would like to know if it's possible to bypass the validation of one property which is using Data Annotations. Since I use the model across multiple pages, there's a check I need in some, but not in others, so I would like it to be ignored. Thaks! ...