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

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

DataAnnotations: Recursively validating an entire object graph

Here’s an alternative to the opt-in attribute approach. I believe this will traverse the object-graph properly and validate everything. public bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results) { bool result = TryValidateObject(obj, results); var properties = obj.GetType().GetProperties().Where(prop => prop.CanRead && !prop.GetCustomAttributes(typeof(SkipRecursiveValidation), false).Any() && prop.GetIndexParameters().Length == 0).ToList(); foreach (var property in properties) { if (property.PropertyType == typeof(string) || … 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

MVC Model require true

I would create a validator for both Server AND Client side. Using MVC and unobtrusive form validation, this can be achieved simply by doing the following: Firstly, create a class in your project to perform the server side validation like so: public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable { public override bool IsValid(object value) { if … Read more

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

Strange. It works for me: Model: public class MyViewModel { [Required] [Remote(“IsEmailAvailable”, “Home”, AdditionalFields = “InitialEmail”)] public string Email { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } public ActionResult IsEmailAvailable(string email, string initialEmail) { … Read more