我正在使用 Linq to SQL 数据注释验证。如果某些字段无效,我会收到验证消息。但是,如果我点击保存,表单仍会提交(使用无效值)。
看法:
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<div id="Content">
@using (Html.BeginForm()) {
....
....
<input type="button" id="btnSave" name="submit" value="Save" />
$("#btnSave").on("click", saveRecord);
function saveRecord() {
//document.forms[0].submit();
$.ajax(
{ type: 'Post' ,
url: '@Url.Action("Save", "Orders")',
data: {
OrderID: '@Model.OrderID',
ShipName: $('.Name input').val(),
ShipAddress: '@Model.ShipAddress',
RequiredDate: '@Model.RequiredDate',
ShipPostalCode: '@Model.ShipPostalCode'
},
dataType: "html",
success: function (data){
//alert ('saved');
}
})
return false;
}