1

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"/>
}
4

2 回答 2

4

确保您的代码中引用了以下 JS 库:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

确保您的 web.config 文件中有以下appSettings条目:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

如果您愿意,还可以在视图而不是配置文件中启用客户端验证:

@{ Html.EnableClientValidation(); }
于 2013-04-21T22:43:09.543 回答
3

就项目需要包含的内容而言,greg84 的答案是正确的答案。但对我来说,我也遇到了 jquery.validate 插件无法与 jquery > 1.9.0 一起使用的问题。一旦我将其更改为 jquery 1.8.3,它就起作用了。

于 2013-07-23T20:38:01.890 回答