簡體   English   中英

MVC 中的范圍驗證僅顯示默認消息

[英]Range validation in MVC Just shows Default Message

我對這個荒謬的問題完全感到困惑,但我必須解決。 我有一個字段,並在其上設置了必需和范圍驗證:

[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>

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

像這樣:顯示范圍驗證的默認消息

這是檢查我的文本框和 javascript 文件檢查和 javascripts

那么,我可能錯在哪里,因為消息顯示不正確?

我終於發現了我的問題,它被引用到這一行:

@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 })

當我使用min和max屬性時,范圍驗證器僅顯示默認消息,而我改為:

@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})

我有一個類似的問題,在 MVC 中對下拉列表進行數據驗證根本不起作用。

[Required]
public int? intEquipmentModelID { get; set; }
public SelectList? EquipmentDescriptionList { get; set; }

通過使用 Range 選項,我能夠“欺騙”系統意識到它丟失了。

[Required]
[Range(1, 2000000, ErrorMessage = "Equipment missing.")]
public int? intEquipmentModelID { get; set; }
public SelectList? EquipmentDescriptionList { get; set; }

大多數下拉菜單都有一個非零的整數 Id。 如果 Select One 與零 (0) 相關聯,則要求它大於零就可以解決問題。 如果下拉列表是一個文本值,那么理論上不允許空白或不允許“選擇一個”也應該可以解決問題,盡管我從未嘗試或測試過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM