0

我有一个简单的登录模型类,几乎没有 DataAnnotation 验证

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

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

End Class

View是一个部分View,如下:

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

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

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

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

4

1 回答 1

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

不确定它是否会有所作为,但尝试像这样添加 true

    <%= Html.ValidationSummary(true)%>
<% Html.EnableClientValidation(true)%>
于 2012-08-24T03:01:48.430 回答