Ajax.BeginForm
是 ASP.NET MVC 中的一个辅助方法,用于创建支持 AJAX 提交的表单。LoadingElementId
是该方法的参数之一,用于指定在表单提交期间显示的加载元素(通常是加载动画或提示信息)。
当 LoadingElementId
不工作时,常见原因包括:
确保你的视图中有一个与 LoadingElementId
参数匹配的元素:
<div id="loadingIndicator" style="display:none;">
<p>Loading...</p>
<!-- 或者使用加载动画 -->
</div>
@using (Ajax.BeginForm("ActionName", "ControllerName",
new AjaxOptions {
HttpMethod = "POST",
LoadingElementId = "loadingIndicator",
OnSuccess = "onSuccess",
OnFailure = "onFailure"
}))
{
<!-- 表单内容 -->
}
确保以下脚本按正确顺序引用:
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
确保没有其他CSS规则覆盖了加载元素的显示/隐藏行为。
<div id="loadingIndicator" style="display:none; position:fixed; top:50%; left:50%;">
<img src="~/Content/loading.gif" alt="Loading..." />
</div>
@using (Ajax.BeginForm("SaveData", "Home",
new AjaxOptions {
HttpMethod = "POST",
LoadingElementId = "loadingIndicator",
OnSuccess = "alert('Success!')",
OnFailure = "alert('Error!')"
}))
{
@Html.TextBoxFor(m => m.Name)
<input type="submit" value="Submit" />
}
@section scripts {
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
}
jquery.unobtrusive-ajax.js
文件已正确加载如果问题仍然存在,可以考虑使用纯jQuery AJAX实现:
$("#myForm").submit(function(e) {
e.preventDefault();
$("#loadingIndicator").show();
$.ajax({
url: $(this).attr("action"),
type: "POST",
data: $(this).serialize(),
success: function(response) {
$("#loadingIndicator").hide();
// 处理成功响应
},
error: function() {
$("#loadingIndicator").hide();
// 处理错误
}
});
});
通过以上步骤,应该能够解决 LoadingElementId
不工作的问题。
没有搜到相关的文章