Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I guess one way of doing it is to sub-class the <code>ModelBindersDictionary</code> class and override the GetBinders(Type modelType, bool fallbackToDefault) method.</p> <pre><code>public class CustomModelBinderDictionary : ModelBinderDictionary { public override IModelBinder GetBinder(Type modelType, bool fallbackToDefault) { IModelBinder binder = base.GetBinder(modelType, false); if (binder == null) { Type baseType = modelType.BaseType; while (binder == null &amp;&amp; baseType != typeof(object)) { binder = base.GetBinder(baseType, false); baseType = baseType.BaseType; } } return binder ?? DefaultBinder; } } </code></pre> <p>Basically walk the class hierarchy until a model binder is found or default to the <code>DefaultModelBinder</code>.</p> <p>The next step is making the framework accept the <code>CustomModelBinderDictionary</code>. As far as I can tell you need to sub-class the following three classes and override the Binders property: <code>DefaultModelBinder</code>, <code>ControllerActionInvoker</code> and <code>Controller</code>. You might want to provide your own static <code>CustomModelBinders</code> class.</p> <p><em>Disclaimer</em>: This is just a rough prototype. I'm not sure if it's actually working, what implications it might have or if it is a sound approach. You might want to download the <a href="http://www.codeplex.com/aspnet" rel="nofollow noreferrer">code</a> for the framework yourself and experiment.</p> <p><strong>Update</strong>:</p> <p>I guess an alternative solution would be to define your own <code>CustomModelBindingAttribute</code>.</p> <pre><code>public class BindToBase : CustomModelBinderAttribute { public override IModelBinder GetBinder() { return new BaseModelBinder(); } } public class CustomController { public ActionResult([BindToBase] Child child) { // Logic. } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload