MVC5 jQuery客户端验证无法正常工作

时间:2017-07-21 12:29:36

标签: jquery asp.net-mvc validation

我有一个MVC视图模型,我想在客户端验证。我在这里已经阅读了很多关于库的顺序的帖子,但我已经反映了这些建议,但仍然无法使验证工作。在表单提交上,我在没有任何验证的情况下到达Customer/CreateCustomer控制器方法。 web.config参数似乎在根目录和Views文件夹中正确设置(两个相关值都设置为true)。

我在Visual Studio 2013中使用MVC5。

这是我的ViewModel:

public class CreateCustomerVm
{
    [Display(Name = "Customer Name")]
    [Required]
    public string Name { get; set; }
}

以下是我的观点:

@using (Html.BeginForm("CreateCustomer", "Customer"))
{
    @Html.ValidationSummary(true, "", new {@class = "text-danger"})
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class = "form-control"}})
            @Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"})
        </div>
    </div>
    <input class="btn btn-primary" type="submit" value="Create"/>
}

呈现部分:

<form action="/Customer/CreateCustomer" method="post">    
    <input name="__RequestVerificationToken" type="hidden" value="rSLp5Rwtq9rqwwrAs8UN_CsgPbXEVG_enkISIhWrkXOokprATgbRcpPNk7o6F6EaxD-9I92f7kAr7TGTFPZe0pUSZ5Rfrz8CoP3ef8-lqhXUhSVAaX7PkmmURR1XcaXQ0" />
<div class="form-horizontal">
    <div class="form-group">
        <label class="control-label col-md-2" for="Name">Customer Name</label>
        <div class="col-md-10">
        <input class="form-control text-box single-line" data-val="true" data-val-required="The Customer Name field is required." id="Name" name="Name" type="text" value="BBC-TV" />
        <span class="field-validation-valid text-danger" data-valmsg-for="Name" data-valmsg-replace="true"></span>
    </div>
</div>
</form>

在我的_layout页面中,我在底部有这个:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

最后我的BundleConfig:

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery.form.js"
                    ));
        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"
                    ));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

0 个答案:

没有答案