MVC中的范围验证只显示默认消息

时间:2017-08-16 13:14:13

标签: c# asp.net-mvc validation model-view-controller data-annotations

我对这个荒谬的问题感到困惑,但我必须解决。 我有一个字段,我在其上设置了必需和范围验证:

[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

这是对我的textBox和javascript文件的检查 inspect and javascripts

那么,我可能错了,因为消息没有正确显示?

1 个答案:

答案 0 :(得分: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 })

当我使用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})