Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom Model Binder inheriting from DefaultModelBinder
    primarykey
    data
    text
    <p>I'm attempting to build a custom model binder for MVC 4 that will inherit from <code>DefaultModelBinder</code>. I'd like it to intercept any interfaces at <em>any</em> binding level and attempt to load the desired type from a hidden field called <code>AssemblyQualifiedName</code>.</p> <p>Here's what I have so far (simplified):</p> <pre><code>public class MyWebApplication : System.Web.HttpApplication { protected void Application_Start() { ModelBinders.Binders.DefaultBinder = new InterfaceModelBinder(); } } public class InterfaceModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType.IsInterface &amp;&amp; controllerContext.RequestContext.HttpContext.Request.Form.AllKeys.Contains("AssemblyQualifiedName")) { ModelBindingContext context = new ModelBindingContext(bindingContext); var item = Activator.CreateInstance( Type.GetType(controllerContext.RequestContext.HttpContext.Request.Form["AssemblyQualifiedName"])); Func&lt;object&gt; modelAccessor = () =&gt; item; context.ModelMetadata = new ModelMetadata(new DataAnnotationsModelMetadataProvider(), bindingContext.ModelMetadata.ContainerType, modelAccessor, item.GetType(), bindingContext.ModelName); return base.BindModel(controllerContext, context); } return base.BindModel(controllerContext, bindingContext); } } </code></pre> <p>Example Create.cshtml file (simplified):</p> <pre><code>@model Models.ScheduledJob @* Begin Form *@ @Html.Hidden("AssemblyQualifiedName", Model.Job.GetType().AssemblyQualifiedName) @Html.Partial("_JobParameters") @* End Form *@ </code></pre> <p>The above partial <code>_JobParameters.cshtml</code> looks at the <code>Model.Job</code>'s properties and builds the edit controls, similar to <code>@Html.EditorFor()</code>, but with some extra markup. The <code>ScheduledJob.Job</code> property is of type <code>IJob</code> (interface).</p> <p>Example ScheduledJobsController.cs (simplified):</p> <pre><code>[HttpPost] public ActionResult Create(ScheduledJob scheduledJob) { //scheduledJob.Job here is not null, but has only default values } </code></pre> <p>When I save the form, it interprets the object type correctly and gets a new instance, but the properties of the object are not being set to their appropriate values.</p> <p>What else do I need to do to this to tell the default binder to take over the property binding of the specified type?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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