问题
My MVC 4 validation is working as it seems, the error messages pop up, but it still submits the form. Any idea what I am missing? Thank you.
public class StatusRequestModel
{
[Required]
[Display(Name = "Ticket Number")]
public int? TicketNumber { get; set; }
[Required]
[Display(Name = "Postal Code")]
public int? PostalCode { get; set; }
}
@using (Html.BeginForm())
{
@Html.ValidationSummary()
@Html.LabelFor(x => x.TicketNumber)
@Html.TextBoxFor(x => x.TicketNumber)
@Html.LabelFor(x => x.PostalCode)
@Html.TextBoxFor(x => x.PostalCode)
<input type="submit" value="Submit" />
<input type="reset" value="Reset"/>
}
回答1:
Ensure you have the following JS libraries referenced in your code:
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
Ensure you have the following appSettings
entries in your web.config file:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
You can also enable client validation in the view instead of the config file if you wish:
@{ Html.EnableClientValidation(); }
回答2:
greg84's answer is the correct answer in terms of what needs to be included to the project. But for me, I also had the problem of the jquery.validate plugin not working with jquery > 1.9.0. Once I changed it to the jquery 1.8.3, it worked.
来源:https://stackoverflow.com/questions/16136900/mvc-4-validation-firing-but-the-form-is-still-submitting