我正在尝试为 MVC 4 构建一个自定义模型绑定器,它将继承自DefaultModelBinder
. 我希望它拦截任何绑定级别的任何接口,并尝试从名为AssemblyQualifiedName
.
这是我到目前为止的内容(简化):
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
&& 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<object> modelAccessor = () => 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);
}
}
示例 Create.cshtml 文件(简化):
@model Models.ScheduledJob
@* Begin Form *@
@Html.Hidden("AssemblyQualifiedName", Model.Job.GetType().AssemblyQualifiedName)
@Html.Partial("_JobParameters")
@* End Form *@
上面的部分_JobParameters.cshtml
查看了Model.Job
的属性并构建了编辑控件,类似于@Html.EditorFor()
,但带有一些额外的标记。该ScheduledJob.Job
属性是类型IJob
(接口)。
示例 ScheduledJobsController.cs(简化):
[HttpPost]
public ActionResult Create(ScheduledJob scheduledJob)
{
//scheduledJob.Job here is not null, but has only default values
}
当我保存表单时,它会正确解释对象类型并获取一个新实例,但对象的属性没有设置为适当的值。
我还需要做什么来告诉默认绑定器接管指定类型的属性绑定?