2017-08-01 97 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進行空值檢查。