0

我完全對這個荒謬的問題感到困惑,但我必須解決。 我有一個領域,我設置所需和範圍驗證就可以了:MVC中的範圍驗證只顯示默認消息

[Required(ErrorMessage = ValidationMessage.InputDataStart + Field.SmtpPort + ValidationMessage.InputDataEnd)]   
[Range(FieldSize.PortNumberMin, FieldSize.PortNumberMax, ErrorMessage = ValidationMessage.InputDataIsOutOfRangeStart + Field.SmtpPort + ValidationMessage.InputDataIsOutOfRangeEnd)] 
public int SmtpPort { get; set; }  

這是NumericBox的代碼:

<div class="Component-Container-Border" style="@BorderWidth"> 
<div class="Component-Label" style="@LabelOfComponentWidth">@Html.DisplayNameFor(model => model)</div> 
<div class="Component-Container-ComponentAndValidation" style="float:right;margin-bottom:0px;"> 
    @Html.TextBoxFor(m => m, new { type = "text", Class = "k-textbox", Value = Value, style = ComponentWidth + ";direction:ltr;border:1px solid #df795a;font-family:MasterFont,tahoma;", MaxLength = Len, Min = MinValue, Max = MaxValue })   
    <script> 
     $("#@FieldName").on("keydown",function(e) {OnKeyDownIntegerNumber(e)}); 
     $("#@FieldName").on("input change keypress",function(e) { 
      $(e.target).valid(); 
     }); 
    </script> 
</div> 
<div style="float:right;"> 
    <input id="ButtonPlus" type="Button" value="+" class="k-button" style="width: 28px; margin: 2px 2px 1px 0px; padding: 0px; height: 26px;font-size: 13px;" onclick="ButtonPlusClick(this, @MaxValue)" /> 
</div> 
@if (Unit.Trim() != "") 
{ 
    <div style="float:right;"> 
     <input id="ButtonMinus" type="Button" value="-" class="k-button" style="width: 28px; margin: 2px 2px 1px 0px; padding: 0px; height: 26px; font-size: 13px;" onclick="ButtonMinusClick(this, @MinValue)" /> 
    </div> 
    <span style="float:right;margin-right:5px;margin-top:5px;">@Unit</span> 
} 
else 
{ 
    <div> 
     <input id="ButtonMinus" type="Button" value="-" class="k-button" style="width: 28px; margin: 2px 2px 1px 0px; padding: 0px; height: 26px; font-size: 13px;" onclick="ButtonMinusClick(this, @MinValue)" /> 
    </div> 
} 
<div style="height:19px;"> 
@Html.ValidationMessageFor(model => model, "", new { @class = "Component-Validation-Message"}) 
</div> 

但是,當我輸入一個數字,出範圍,驗證顯示我的默認範圍消息,而不是我的自定義消息。

這樣的: Showing default message of range validation

這是檢驗我的文本框和JavaScript文件 inspect and javascripts

所以,在這裏我可能是錯的,因爲這些消息顯示不正確?

+0

是否爲該項目啓用了不顯眼的驗證?如果是這樣,你可能需要擴展jQuery的validate插件,你也可以提供一個瀏覽器的圖片檢查元素與生成的數據屬性。 –

+0

感謝您的考慮,但其他驗證(例如required或StringLength)正常工作 –

回答

0

我終於找到了我的問題,有人提到這條線:

@Html.TextBoxFor(m => m, new { type = "text", Class = "k-textbox", Value = Value, style = ComponentWidth + ";direction:ltr;border:1px solid #df795a;font-family:MasterFont,tahoma;", MaxLength = Len, Min = MinValue, Max = MaxValue }) 

當我使用的最小和最大屬性,範圍驗證只鞋缺省消息,我改成這樣:

@Html.TextBoxFor(m => m, new { type = "text", Class = "k-textbox", Value = Value, style = ComponentWidth + ";direction:ltr;border:1px solid #df795a;font-family:MasterFont,tahoma;", MaxLength = Len})