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