2017-08-01 51 views
0

对于Kendo数字文本框的范围验证,即使指定了相同内容,也不会显示自定义消息。Kendo数字文本框的范围验证不会采用默认消息

Range数据注释是这样定义的。

[Required(ErrorMessage = "Enter length")] 
[Range(1, 10, ErrorMessageName = "{0} should be from {1} to {2}")] 
public int Length { get; set; } 

剃刀标记是,

@Html.Kendo().NumericTextBoxFor(m => m.Length) 

但尽管如此,被显示为验证消息,

请输入小于或等于10。

的值

而不是,

长度应为1至10。


这个问题,仅作参考。

此问题已被询问。 Kendo numeric textbox range validator message

但由于同样没有被接受的答案,甚至答案都不能解释,所以我会在这里添加答案。

回答

0

此问题是因为Kendo覆盖了文本框的输入类型,并且为Kendo验证程序添加了不同的消息,即使您不使用它,它也是Kendo重叠的不显眼验证。

要解决这个问题,并实现通用的Range属性,我必须设置自定义属性并将其注册为Range适配器。

在Global.asax中

DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RangeAttribute), 
typeof(CustomRangeAttributeAdapter)); 

我在我的自定义验证命名空间来定义CustomRangeAttributeAdapter这样,

public class CustomRangeAttributeAdapter : RangeAttributeAdapter 
    { 
     public CustomRangeAttributeAdapter(ModelMetadata metadata, 
              ControllerContext context, 
              RangeAttribute attribute) 
       : base(metadata, context, attribute) 
     { 
      attribute.ErrorMessageResourceName = <Resource_Name_Here>; 
      attribute.ErrorMessageResourceType = typeof(<Resource_File_Name>); 
      metadata.DataTypeName = DataType.Currency.ToString(); 
     } 
    } 

我不得不设置DataType,以确保它不会采取默认类型。在此之后,您可以按原样使用Range属性。如果您需要显示自定义消息,请对ErrorMessageErrorMessageName进行空值检查。