如何改变“数据-VAL-号码”消息验证MVC中,而它是由@Html帮手产生 [英] How to change 'data-val-number' message validation in MVC while it is generated by @Html helper

查看:280
本文介绍了如何改变“数据-VAL-号码”消息验证MVC中,而它是由@Html帮手产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这个模型:

Public Class Detail
    ...
    <DisplayName("Custom DisplayName")>
    <Required(ErrorMessage:="Custom ErrorMessage")>
    Public Property PercentChange As Integer
    ...
end class

和视图:

@Html.TextBoxFor(Function(m) m.PercentChange)

将进入这个网站:

will proceed this html:

   <input data-val="true" 
    data-val-number="The field 'Custom DisplayName' must be a number." 
    data-val-required="Custom ErrorMessage"     
    id="PercentChange" 
    name="PercentChange" type="text" value="0" />

我要自定义的数据-VAL-号我的猜测已经产生,因为 PercentChange 是一个错误信息整数。我一直在寻找这样的属性来改变它,范围或任何相关不起作用。结果
我知道有在不显眼的编辑的js文件本身或客户端覆盖它一个机会。我想改变,就像其他数据-VAL-号的错误消息,在服务器端。

I want to customize the data-val-number error message which I guess has generated because PercentChange is an Integer. I was looking for such an attribute to change it, range or whatever related does not work.
I know there is a chance in editing unobtrusive's js file itself or override it in client side. I want to change data-val-number's error message just like others in server side.

推荐答案

这是不是要容易。默认邮件存储为嵌入的资源到 System.Web.Mvc 组装和所取是一个内部密封的内部类的私有静态方法方法(<$c$c>System.Web.Mvc.ClientDataTypeModelValidatorProvider+NumericModelValidator.MakeErrorString).这是因为如果在微软的家伙编码这种躲在一个绝密:-)

This is not gonna be easy. The default message is stored as an embedded resource into the System.Web.Mvc assembly and the method that is fetching is a private static method of an internal sealed inner class (System.Web.Mvc.ClientDataTypeModelValidatorProvider+NumericModelValidator.MakeErrorString). It's as if the guy at Microsoft coding this was hiding a top secret :-)

您可以看看下面的博客文章其描述了可能的解决方案。你基本上需要更换现有的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.clientdatatypemodelvalidatorprovider.aspx\">ClientDataTypeModelValidatorProvider使用自定义的。

You may take a look at the following blog post which describes a possible solution. You basically need to replace the existing ClientDataTypeModelValidatorProvider with a custom one.

如果你不喜欢的铁杆编码,你需要做的,你也可以用字符串替换此整数您的视图模型里面有一个自定义的验证属性,它会做解析,并提供了一​​个自定义错误消息(甚至可以本地化)。

If you don't like the hardcore coding that you will need to do you could also replace this integer value inside your view model with a string and have a custom validation attribute on it which would do the parsing and provide a custom error message (which could even be localized).

这篇关于如何改变“数据-VAL-号码”消息验证MVC中,而它是由@Html帮手产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆