ASP.NET MVC - 提交不使用验证

时间:2015-02-11 11:24:24

标签: jquery asp.net-mvc validation

我有一个ajax表单,使用jquery的一些验证规则但由于某种原因提交按钮不起作用。

<div class="modal-body">
            @using (Ajax.BeginForm("SaveNotices", "Lookup", null, new { @id = "noticeForm"}))
            {
                {
                    // Html.EnableClientValidation(true);
                    // Html.ValidationSummary(true);
                }

                <div class="form-group">
                    @Html.LabelFor(p => p.Consumer.FirstName)
                    @Html.TextBoxFor(p => p.Consumer.FirstName, new { @id = "firstN", @class = "form-control" })
                    @Html.ValidationMessageFor(p => p.Consumer.FirstName)
                </div>
                <div class="form-group">
                    @Html.LabelFor(p => p.Consumer.LastName)
                    @Html.TextBoxFor(p => p.Consumer.LastName, new { @id = "lastN", @class = "form-control" })
                    @Html.ValidationMessageFor(p => p.Consumer.LastName)
                </div>

                ... more code here ... (this does not influence btw)

                <input type="submit" class="btn btn-primary" value="Save" id="ntSubmit" onclick="checkValid()"/>
            }
        </div>

这是我的模特

public class Consumer
{
    // [Required, MaxLength(25)]

    [Required]
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [Required]
    [DisplayName("Last Name")]
    public string LastName { get; set; }
}

public class PlateViewModel
{
    public List<Notice> Notices { get; set; }
    public Consumer Consumer { get; set; }
}

主要表单位于bootstrap-modal中。 脚本包括在内(@ Scripts.Render(“〜/ bundles / jqueryval”)) 我也试图看看ajax是否完成并调用基于OnSuccess,OnComplete或OnFailure的方法,但它永远不会去那里。控制器既不会被击中

内部web.config

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

我做错了什么?

Ajax选项: var ajaxOpt = new AjaxOptions { HttpMethod = "POST", OnSuccess = "redValid", OnComplete = "redValid", OnFailure = "fail" };

使用这些选项调用Ajax表单(我在第一个代码中显示的不是null)

CheckValid:

var checkValid = function () {
    if ($("#firstN").valid() && $("#lastN").valid()) {
        console.log("Inside validation");
        // $("#myModal").modal('hide');
    };
};

0 个答案:

没有答案