asp.net-mvc – MVC 4忽略DefaultModelBinder.ResourceClassKey
发布时间:2020-05-23 14:31:01 所属栏目:asp.Net 来源:互联网
导读:将资源文件添加到具有PropertyValueRequired键的App_GlobalResources中,并将DefaultModelBinder.ResourceClassKey更改为文件名对MVC 4没有影响。字符串{0}字段是必需的,从未更改。 我不想在每个必填字段上设置资源类类型和密钥。 我错过了什么吗? 编辑:
|
将资源文件添加到具有PropertyValueRequired键的App_GlobalResources中,并将DefaultModelBinder.ResourceClassKey更改为文件名对MVC 4没有影响。字符串{0}字段是必需的,从未更改。
编辑: 我对Darin Dimitrov的代码做了一个小的修改,以保持“必需”的自定义工作: 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";
}
}
}
解决方法这不是特定于ASP.NET MVC 4.它在ASP.NET MVC 3中是一样的。您不能使用DefaultModelBinder.ResourceClassKey设置所需的消息,只能使用PropertyValueInvalid。实现您要查找的一种方法是定义一个自定义的RequiredAttributeAdapter: public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(
ModelMetadata metadata,RequiredAttribute attribute
) : base(metadata,attribute)
{
attribute.ErrorMessageResourceType = typeof(Messages);
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
您将在Application_Start中注册: DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute),typeof(MyRequiredAttributeAdapter)
);
现在当一个不可为空的字段未分配一个值时,错误消息将来自Messages.PropertyValueRequired,其中必须在App_GlobalResources中定义Messages.resx。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – asp.net mvc 3 – ajax表单提交和验证
- asp.net-mvc – 使用MVC将数据导入局部视图或布局
- asp.net – 为什么要模拟HttpContext,如果它可以构造?
- asp-classic – 经典ASP的好IDE?
- MVC3Razor – 将DateTime字符串从“mm / dd / yyyy 12:00:0
- asp.net – system.web.compilation.debug与system.codedom
- .net – MVC – 是模型查看还是控制器查看?
- 如何在ASP.NET MVC控制器(ActionResult)中更改返回的Conten
- asp.net-mvc – 模型绑定和GET请求?
- asp.net-mvc – 在使用ASP.Net MVC的Html.TextBoxFor时,如何
推荐文章
站长推荐
- 如何使ASP.NET服务器控件获得最短的ID?
- 免费ASP.Net和/或CSS主题
- asp.net – FormsAuthentication.SetAuthCookie(
- asp.net-mvc-4 – 什么是antlr3,为什么默认情况下
- 为Asp.Net MVC中的特定Controller或Action启用SS
- asp.net – 哪些移动浏览器支持javascript(和Aja
- asp.net-mvc – Sitecore和ASP.net MVC
- asp.net-mvc – ASP.NET MVC 4 – 301重定向Rout
- Asp.net从Https重定向到Http
- 如何为Asp.Net中的所有子文件夹注册HttpHandler?
热点阅读
