Not getting validation Error Messages from DataAnnotation Validations

寵の児 提交于 2019-12-13 04:12:51

问题


I am have a simple model class for Login, with few DataAnnotation Validations

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

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

End Class

View is a partial View and is as follows:

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

I believe I have done all the things needed for validation message to show up, but it does not, neither does clientside validation. I have also included the following scripts, on the head region of 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>

What is causing this problem? And what is the solution?


回答1:


you need to add some error messages to your annotation

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

not sure if it will make a difference but try adding true like this

    <%= Html.ValidationSummary(true)%>
<% Html.EnableClientValidation(true)%>


来源:https://stackoverflow.com/questions/12102433/not-getting-validation-error-messages-from-dataannotation-validations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!