Что не так с моим кодом, я что-то упустил?Каждый раз, когда я нажимаю кнопку, form.valid () всегда возвращает true, даже если поле ввода пустое.Чего мне не хватает?
КОД ЧТМЛ:
@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>