Using DataAnnotations to compare two model properties

Make sure that your project references system.web.mvc v3.xxxxx. Then your code should be something like this: using System.Web.Mvc; . . . . [Required(ErrorMessage = “This field is required.”)] public string NewPassword { get; set; } [Required(ErrorMessage = “This field is required.”)] [Compare(nameof(NewPassword), ErrorMessage = “Passwords don’t match.”)] public string RepeatPassword { get; set; }

Unit Testing ASP.NET DataAnnotations validation

I posted this in my blog post: using System.ComponentModel.DataAnnotations; // model class public class Fiz { [Required] public string Name { get; set; } [Required] [RegularExpression(“.+@..+”)] public string Email { get; set; } } // in test class [TestMethod] public void EmailRequired() { var fiz = new Fiz { Name = “asdf”, Email = null … Read more

Validation does not work when I use Validator.TryValidateObject

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer), typeof(CustomerMetadata)), typeof(Customer)); var isValid = Validator.TryValidateObject(new Customer(), context, results, true);

DataAnnotations dynamically attaching attributes

MVC has a hook to provide your own ModelValidatorProvider. By default MVC 2 uses a sub class of ModelValidatorProvider called DataAnnotationsModelValidatorProvider that is able to use System.DataAnnotations.ComponentModel.ValidationAttribute attributes for validation. The DataAnnotationsModelValidatorProvider uses reflection to find all the ValidationAttributes and simply loops through the collection to validate your models. All you need to do is … Read more

Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

Why can’t I reference System.ComponentModel.DataAnnotations?

You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)

DateTime (date and hour) validation with Data Annotation

You could write your own ValidationAttribute and decorate the property with it. You override the IsValid method with your own logic. public class MyAwesomeDateValidation : ValidationAttribute { public override bool IsValid(object value) { DateTime dt; bool parsed = DateTime.TryParse((string)value, out dt); if(!parsed) return false; // eliminate other invalid values, etc // if contains valid hour … Read more

Entity Framework 4.1 InverseProperty Attribute

I add an example for the InversePropertyAttribute. It cannot only be used for relationships in self referencing entities (as in the example linked in Ladislav’s answer) but also in the “normal” case of relationships between different entities: public class Book { public int ID {get; set;} public string Title {get; set;} [InverseProperty(“Books”)] public Author Author … Read more