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

Validating DataAnnotations with Validator class

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(Persona), typeof(Persona_Validation)), typeof(Persona)); ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, context, results, true);

Data Annotations for validation, at least one required field?

I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more

The field must be a number. How to change this message to another language?

If you happen to be using ASP.NET MVC 4 onwards, check this post: Localizing Default Error Messages in ASP.NET MVC and WebForms Basically you have to add the following piece of code in your Application_Start() method in Global.asax: ClientDataTypeModelValidatorProvider.ResourceClassKey = “Messages”; DefaultModelBinder.ResourceClassKey = “Messages”; Right click your ASP.NET MVC project in Solution Explorer inside Visual … Read more

Validator.TryValidateObject Not Validating RangeAttribute

Ah so it would seem I need to specify validateAllProperties = true Validator.TryValidateObject(question, ctx, results, true); Incidentally what was throwing me off was the fact I had an abstract base class with another property in it and without validateAllProperties the Validator will stop on the first error of ALL superclasses too. So you will get … Read more

Custom model validation of dependent properties using Data Annotations

MVC2 comes with a sample “PropertiesMustMatchAttribute” that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this: [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ … Read more

What is use of UIHint attribute in MVC [duplicate]

UIHintAttribute Specifies the template or user control that Dynamic Data uses to display a data field. This is the MSDN description of UIHintAttribute. It firstly introduced for Dynamic Data applications and ASP.NET MVC also adapted it. If you annotate a property with UIHint attribute and use EditorFor or DisplayFor inside your views, ASP.NET MVC framework … Read more

Get [DisplayName] attribute of a property in strongly-typed way

There are two ways to do this: Models.Test test = new Models.Test(); string DisplayName = test.GetDisplayName(t => t.Name); string DisplayName = Helpers.GetDisplayName<Models.Test>(t => t.Name); The first one works by virtue of writing a generic extension method to any TModel (which is all types). This means it will be available on any object and not just … Read more