This example show how to send object through JSON message and then bind it to class. DefaultModelBinder did not bind abstract class objects (throwing MissingMethodException: Cannot create an abstract class). My solution extends DefaultModelBinder to support abstract classes. It is done by creating object of concrete type and then casting it to abstract class. Concrete type is recognized by obtaining request field named: Type. For example if model type is abstract Content, following JSON request: { Name: ‘Hello World’, Type:’HelloContent’ } will create HelloContent class, assign Name field and cast it to Content class.

The following class is responsible for above behavior.

public class EnhancedDefaultModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        Type type = modelType;
        if (modelType.IsGenericType)
        {
            Type genericTypeDefinition = modelType.GetGenericTypeDefinition();
            if (genericTypeDefinition == typeof(IDictionary))
            {
                type = typeof(Dictionary).MakeGenericType(modelType.GetGenericArguments());
            }
            else if (((genericTypeDefinition == typeof(IEnumerable<>)) || (genericTypeDefinition == typeof(ICollection<>))) || (genericTypeDefinition == typeof(IList<>)))
            {
                type = typeof(List<>).MakeGenericType(modelType.GetGenericArguments());
            }
            return Activator.CreateInstance(type);
        }
        else if(modelType.IsAbstract)
        {
            string concreteTypeName = bindingContext.ModelName + ".Type";
            var concreteTypeResult = bindingContext.ValueProvider.GetValue(concreteTypeName);

            if (concreteTypeResult == null)
                throw new Exception("Concrete type for abstract class not specified");

            type = Assembly.GetExecutingAssembly().GetTypes().SingleOrDefault(t => t.IsSubclassOf(modelType) && t.Name == concreteTypeResult.AttemptedValue);

            if (type == null)
                throw new Exception(String.Format("Concrete model type {0} not found", concreteTypeResult.AttemptedValue));

            var instance = Activator.CreateInstance(type);
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, type);
            return instance;
        }
        else
        {
            return Activator.CreateInstance(modelType);
        }
    }
}

In example project is shown how to bind complex models that can contain multiple abstract classes and collection of those objects. The class hierarchy of project is shown below.


Here is project code available for download.