模式内的ASP.NET MVC5 CRUD验证无法正常工作

时间:2019-06-09 13:04:50

标签: jquery ajax asp.net-mvc-5

内部模态验证工作正常,但是实际问题是表单始终有效()= false结果,因此请不要点击控制器方法

我尝试了unobtrusive.parse方法进行验证并可以正常工作,但是如果进入(formRef.IsValid()){}函数并且始终为false,则不要进入内部

@model GridCRUDAjax.Models.ChapterCategory
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<a class="btn btn-success btn-sm text-light" onclick="AddView()" href="#"><span class="fa fa-plus p-1"></span>Add New</a>
<hr />
<table id="ChaptCategoriesTable" class="table  table-striped table-hover table-light" style="width:100%"></table>

<script>
    var dtCatList;
    $(document).ready(function () {
        dtCatList=  $("#ChaptCategoriesTable").DataTable({
            ajax: {
                type: "GET",
                url: "@Url.Action("GetData", "Home")",
                dataType: "JSON"
            },
            columns: [
                { title: "Category", data: "CatName", searchable: true, width:"500" }
            ],
            language: {
                processing:"<img src='~/Images/loader.gif'>",
                emptyTable: "No Data found ,Add Data..!"
            }

        });
    });
    function AddView() {
        $("#BtnUpdate").hide();
        $("#BtnSubmit").show();
        $("#myModal").modal('show');
        $("#ChaptCatId").val("");
        $("#CatName").val("");
        return false;
    }
    function BtnSubmit() {

       var formRef = $("#formSubmit");
       formRef.removeData('validator');
       formRef.removeData('unobtrusiveValidation');
        $.validator.unobtrusive.parse(formRef);

        if (formRef.IsValid()) {
           var data = formRef.serialize();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("PostData","Home")',
                data: data,
                dataType: 'JSON',
                success: function (response) {
                    $.notify(response.message, { className: 'success', globalPosition: 'top center' })
                    dtCatList.ajax.reload();
                    $("#myModal").modal('hide');

                }, error: function (msg) {
                    alert("Error while Saving");
                }
           });
       } 

    }

</script>



<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Add New Category</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("PostData", "Home", FormMethod.Post, new { @id = "formSubmit" }))
                {
                    @Html.HiddenFor(model => model.ChaptCatId, new { @id = "ChaptCatId" })
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(model => model.CatName, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col">
                            @Html.TextBoxFor(model => model.CatName, new { @class = "form-control", @name = "CatName" })
                            @Html.ValidationMessageFor(model=>model.CatName,"",new { @class="text-danger"})
                        </div>
                    </div>
                }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="BtnSubmit()" id="BtnSubmit">Add</button>
            </div>
        </div>
    </div>
</div>

如果(formRef.IsValid()){}

1 个答案:

答案 0 :(得分:0)

试试这个-

$("#form_id").valid();