未从DataAnnotation验证中获取验证错误消息

时间:2012-08-24 02:21:43

标签: asp.net vb.net validation data-annotations html-helper

我有一个简单的Login模型类,只有很少的DataAnnotation Validations

Public Class LoginUser
    <Required()> _
    Public Property UserName As String

    <Required()> _
    <StringLength(8)> _
    Public Property Password As String

End Class

视图是部分视图,如下所示:

<% Using (Html.BeginForm("Login", "User", FormMethod.Post))%>
<% Html.EnableClientValidation()%>

<%= Html.ValidationSummary() %>
<table ID="loginTable" runat="server">
    <tr>
        <td>
            <%= Html.LabelFor(Function(x) x.UserName)%>
        </td>
    </tr>
    <tr>
        <td>
            <%= Html.TextBoxFor(Function(x) x.UserName)%>
            <%= Html.ValidationMessageFor(Function(x) x.UserName) %>
        </td>
    </tr>
    <tr>
        <td>
            <%= Html.LabelFor(Function(x) x.Password)%>
        </td>
    </tr>
    <tr>
        <td>
            <%= Html.TextBoxFor(Function(x) x.Password)%>
            <%= Html.ValidationMessageFor(Function(x) x.Password)%>
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>
<% End Using%>   

我相信我已经完成了验证消息显示所需的所有内容,但它没有,客户端验证也没有。我还在Master Page的头部区域包含了以下脚本

<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script> 
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

是什么导致了这个问题?什么是解决方案?

1 个答案:

答案 0 :(得分:0)

您需要在注释中添加一些错误消息

Public Class LoginUser
    <Required()(ErrorMessage:="Username is required.")> _
    Public Property UserName As String

    <Required(ErrorMessage:="Pssword is required.")> _
    <StringLength(8)> _
    Public Property Password As String

End Class

不确定它是否有所作为,但尝试添加如此

的真实
    <%= Html.ValidationSummary(true)%>
<% Html.EnableClientValidation(true)%>