我有一个带有 ajax 表单的页面。当我按下提交时,它应该加载一些加载 gif。请参阅以下 HTML 和 javascript:
@using (Ajax.BeginForm("ActionName", "ControllerName", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "DivToUpdate"}, new { id = "myForm"})) { @Html.AntiForgeryToken();
@Html.LabelFor(model => model.PagerSize)
@Html.EditorFor(model => model.PagerSize, new { htmlAttributes = new { @class = "form-control myForm", style = "width:100px" } })
@Html.ValidationMessageFor(model => model.PagerSize)
<input id = "ajaxFormSubmitButton" type = "submit" />
}
<script>
$("#myForm").submit(function (event) {
DisplayLoading();
});
</script>
它工作正常,直到我的模型无效。例如,当我的寻呼机大小超出我的范围时,它会继续显示我的加载 gif,因为页面没有加载任何其他内容。是否可以仅在 ModelState.IsValid 时加载 DisplayLoading?
例如这样的事情:
<script>
$("#myForm").submit(function (event) {
If (MyMagicVariableWhichKnowsMyModelStateIsValid){
DisplayLoading();
}
});
</script>
最佳答案
您可以检查表单是否有效,如下所示:
$("#myForm").submit(function (event) {
if ($("#myForm").valid()){
DisplayLoading();
}
});
关于javascript - javascript 中的 asp.net MVC 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43847945/