This project is read-only.
1
Vote

ModelBinder and MetaDataAttribute Problem

description

Modelbinder can not load meta data from another class
in entityframework EDMX files create model file automatically and we need to add meta data class

i fix in my source based on codes from here:
http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/


changes: (ModelBinder.cs)
 static void BindControl(object obj, Control page)
if (obj == null)
                return;

            var metadataAttrib = obj.GetType().GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().FirstOrDefault();
            var buddyClassOrModelClass = metadataAttrib != null ? metadataAttrib.MetadataClassType : obj.GetType();

            var properties = buddyClassOrModelClass.GetProperties(BindingFlags.Instance | BindingFlags.Public);
and some where that load complex type

comments

mahdiit wrote Mar 23, 2013 at 7:37 PM

you need to change every four section that load Properties with this function:
private static Type GetClassOrModelType(Type baseType)
        {
            var metadataAttrib = baseType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().FirstOrDefault();
            return metadataAttrib != null ? metadataAttrib.MetadataClassType : baseType;
        }
and then change
var properties = type.GetProperties();
to this
var properties = GetClassOrModelType(type).GetProperties();

wrote Apr 1, 2013 at 7:34 AM

wrote Nov 29 at 12:17 PM