Form.valid()始终在ASP .Net核心客户端验证中返回true

时间:2018-06-07 07:48:04

标签: javascript asp.net-mvc validation asp.net-core

我的代码出了什么问题,我错过了什么?每次单击该按钮时,即使输入字段为空,form.valid()也始终返回true。我错过了什么?

CHTML代码:

    @section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script type="text/javascript">
        $(document).ready(function(){
            $('#btnAddEntry').click(function (event) {
                event.preventDefault();
                var $form = $('form');
                alert($form.valid());
                if ($form.valid()) {
                    var rows = {};
                    $('#CreateForm .form-control').each(function () {
                        rows["'" + this.id + "'"] = this.value;
                    });
                    $.ajax({
                        type: "POST",
                        url: "@Url.Action("Create1", "Books")",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: JSON.stringify(rows),
                        success: function (data) {
                            console.log(data);
                        }
                    });
                }
            });
        });
    </script>
}

这是我的型号代码:

public class Book
    {
        public int Id { get; set; }
        [Required]
        public int Year { get; set; }
        [Required]
        public int Month { get; set; }
        [Required]
        public string Document_Code { get; set; }
        [Required]
        public string GLA_Code { get; set; }
        [Required]
        public string Expense_Code { get; set; }
        [Required]
        public string Function_Code { get; set; }
        [Required]
        public string Document_Reference_No { get; set; }
    }

已经检查_ValidationScriptsPartial包括

<environment include="Development">
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>

0 个答案:

没有答案