C# 自定义模型绑定器继承自 DefaultModelBinder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16747320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Model Binder inheriting from DefaultModelBinder
提问by Albert Bori
I'm attempting to build a custom model binder for MVC 4 that will inherit from DefaultModelBinder. I'd like it to intercept any interfaces at anybinding level and attempt to load the desired type from a hidden field called AssemblyQualifiedName.
我正在尝试为 MVC 4 构建一个自定义模型绑定器,它将继承自DefaultModelBinder. 我想它在拦截任何接口的任何结合水平并尝试从称为一个隐藏字段加载所需的类型AssemblyQualifiedName。
Here's what I have so far (simplified):
这是我到目前为止所拥有的(简化):
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);
}
}
Example Create.cshtml file (simplified):
示例 Create.cshtml 文件(简化):
@model Models.ScheduledJob
@* Begin Form *@
@Html.Hidden("AssemblyQualifiedName", Model.Job.GetType().AssemblyQualifiedName)
@Html.Partial("_JobParameters")
@* End Form *@
The above partial _JobParameters.cshtmllooks at the Model.Job's properties and builds the edit controls, similar to @Html.EditorFor(), but with some extra markup. The ScheduledJob.Jobproperty is of type IJob(interface).
上面的部分_JobParameters.cshtml查看Model.Job的属性并构建编辑控件,类似于@Html.EditorFor(),但带有一些额外的标记。该ScheduledJob.Job属性属于类型IJob(接口)。
Example ScheduledJobsController.cs (simplified):
ScheduledJobsController.cs 示例(简化版):
[HttpPost]
public ActionResult Create(ScheduledJob scheduledJob)
{
//scheduledJob.Job here is not null, but has only default values
}
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.
当我保存表单时,它会正确解释对象类型并获取一个新实例,但对象的属性没有设置为其适当的值。
What else do I need to do to this to tell the default binder to take over the property binding of the specified type?
我还需要做什么来告诉默认绑定器接管指定类型的属性绑定?
采纳答案by Albert Bori
This articleshowed me that I was over-complicating the model binder. The following code works:
这篇文章向我展示了我过于复杂的模型活页夹。以下代码有效:
public class InterfaceModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType.IsInterface)
{
Type desiredType = Type.GetType(
EncryptionService.Decrypt(
(string)bindingContext.ValueProvider.GetValue("AssemblyQualifiedName").ConvertTo(typeof(string))));
bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, desiredType);
}
return base.BindModel(controllerContext, bindingContext);
}
}
回答by Joe Kahl
With MVC 4 it is easy to override the messages, if that is all you might need in a custom model binder:
使用 MVC 4 很容易覆盖消息,如果这就是您在自定义模型绑定器中可能需要的全部内容:
protected void Application_Start(object sender, EventArgs e)
{
//set mvc default messages, or language specifc
ClientDataTypeModelValidatorProvider.ResourceClassKey = "ValidationMessages";
DefaultModelBinder.ResourceClassKey = "ValidationMessages";
}
Then create resource file named ValidationMessageswith entries like this:
然后创建ValidationMessages以如下条目命名的资源文件:
NAME: FieldMustBeDate
VALUE: The field {0} must be a date.
NAME: FieldMustBeNumeric
VALUE: The field {0} must be a number
.
.
We did this for a compliance failure. Our security scan did not like that a javascriptinjection would come back and appear in the Validation Messages and execute. By using this implementation we are overriding the default messages which return the user provided value.
我们这样做是为了合规性失败。我们的安全扫描不希望javascript注入返回并出现在验证消息中并执行。通过使用此实现,我们将覆盖返回用户提供值的默认消息。

