我的代码出了什么问题,我错过了什么?每次单击该按钮时,即使输入字段为空,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>