MVC 4 Validation firing, but the form is still submitting?

帅比萌擦擦* 提交于 2020-01-05 07:25:10

问题


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

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