MVC 4忽略DefaultModelBinder.ResourceClassKey [英] MVC 4 ignores DefaultModelBinder.ResourceClassKey
问题描述
添加了 PropertyValueRequired
键资源文件 App_GlobalResources文件
和不断变化的 DefaultModelBinder.ResourceClassKey
来的文件名对MVC 4没有影响字符串 {0}字段是必需的
是从未改变。
我不想设置在每个领域需要的资源类类型和密钥。
我缺少的东西吗?
编辑:
我做了对达林季米特洛夫code一个小的修改,以保持必需的自定义工作:
公共类MyRequiredAttributeAdapter:RequiredAttributeAdapter
{
公共MyRequiredAttributeAdapter(
ModelMetadata元,
ControllerContext的背景下,
属性RequiredAttribute标签
)
:基地(元数据,内容,属性)
{
如果(attribute.ErrorMessageResourceType == NULL)
{
attribute.ErrorMessageResourceType = typeof运算(消息);
}
如果(attribute.ErrorMessageResourceName == NULL)
{
attribute.ErrorMessageResourceName =PropertyValueRequired;
}
}
}
这是不特定于ASP.NET MVC 4.在ASP.NET同样的MVC 3,使用你不能设置所需的信息 DefaultModelBinder.ResourceClassKey
,只有 PropertyValueInvalid
。
实现你所寻找的一种方法是定义一个自定义 RequiredAttributeAdapter
:
公共类MyRequiredAttributeAdapter:RequiredAttributeAdapter
{
公共MyRequiredAttributeAdapter(
ModelMetadata元,
ControllerContext的背景下,
属性RequiredAttribute标签
):碱(元数据,上下文,属性)
{
attribute.ErrorMessageResourceType = typeof运算(消息);
attribute.ErrorMessageResourceName =PropertyValueRequired;
}
}
您将在注册的Application_Start
:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof运算(RequiredAttribute标签),
typeof运算(MyRequiredAttributeAdapter)
);
现在当一个非空的字段不分配一个值,错误信息将来自 Messages.PropertyValueRequired
,其中 Messages.resx
必须在 App_GlobalResources文件
。
Adding a resource file to App_GlobalResources
with a PropertyValueRequired
key and changing DefaultModelBinder.ResourceClassKey
to the file name has no effect on MVC 4. The string The {0} field is required
is never changed.
I don't want to set the resource class type and key on every required field.
Am I missing something?
Edit:
I've made a small modification on Darin Dimitrov's code to keep Required customizations working:
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(
ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute
)
: base(metadata, context, attribute)
{
if (attribute.ErrorMessageResourceType == null)
{
attribute.ErrorMessageResourceType = typeof(Messages);
}
if (attribute.ErrorMessageResourceName == null)
{
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
}
This is not specific to ASP.NET MVC 4. It was the same in ASP.NET MVC 3. You cannot set the required message using DefaultModelBinder.ResourceClassKey
, only the PropertyValueInvalid
.
One way to achieve what you are looking for is to define a custom RequiredAttributeAdapter
:
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(
ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute
) : base(metadata, context, attribute)
{
attribute.ErrorMessageResourceType = typeof(Messages);
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
that you will register in Application_Start
:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute),
typeof(MyRequiredAttributeAdapter)
);
Now when a non-nullable field is not assigned a value, the error message will come from Messages.PropertyValueRequired
where Messages.resx
must be defined inside App_GlobalResources
.
这篇关于MVC 4忽略DefaultModelBinder.ResourceClassKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!