C# 从Ajax调用返回空间视图后隐藏引导模式
当我调用ajax时,我会显示我的模式(options.beforeSend)。但是,当我得到一个ajax结果错误(options.error)时,我想隐藏这个模式。我试过了,但没有成功 Index.cshtmlC# 从Ajax调用返回空间视图后隐藏引导模式,c#,asp.net-core,bootstrap-modal,asp.net-ajax,C#,Asp.net Core,Bootstrap Modal,Asp.net Ajax,当我调用ajax时,我会显示我的模式(options.beforeSend)。但是,当我得到一个ajax结果错误(options.error)时,我想隐藏这个模式。我试过了,但没有成功 Index.cshtml @* Modal - load spin *@ <div class="modal fade" id="itemLoader" tabindex="-1" role="dialog" aria-labe
@* Modal - load spin *@
<div class="modal fade" id="itemLoader" tabindex="-1" role="dialog" aria-labelledby="ModalLabel2" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
</div>
<div class="modal-body d-inline text-center">
<div class="spinner-border spinner-border-sm text-info" role="status">
<span class="sr-only small"></span>
</div>
<span class="far fa-dizzy fa-3x text-secondary" style="display:none;"></span>
<label id="ModalStatus">Loading...</label>
</div>
</div>
</div>
</div>
如果出现错误(在部分视图返回后,“来自模型状态”无效),我将尝试隐藏模型,但我无法:
options.error = function (res) {
// When my modelState is not valid, return partial view with required messages (working fine)
$('#chkForm').html(res.responseText);
// But I can't hide the modal (does not work)
var modal = $("#itemLoader");
modal.hide();
// hide modal (does not work)
$('#itemLoader').modal('hide');
$('#itemLoader').hide();
};
我已尝试在modal上关闭按钮,但也没有成功:
<button id="btnclosemodal" class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
// Does not work
$('#btnclosemodal').click();
// Does not work
$("#btnclosemodal").trigger("click");
×
//不起作用
$('#btnclosemodal')。单击();
//不起作用
$(“#btnclosemodal”)。触发器(“单击”);
您可以尝试切换模式开启错误,如下所示:
$('itemLoader').modal('toggle')如果请求失败,将调用code>error
函数。我认为在这里它不会进入错误函数,因为它通常返回局部视图。您可以使用f12并在“源”选项卡中调试它
相反,我认为您应该将模式隐藏在success函数中。控制台中有错误吗?主线程上的同步XMLHttpRequest被弃用,因为它会对最终用户的体验产生有害影响。如需更多帮助,请查看。未能加载资源:服务器以404()类型的状态响应错误:无法设置未定义的属性“unobtrusive”加载资源失败:服务器以404()范围的状态响应错误:超出了最大调用堆栈大小。因此,此时,我想知道您是否在xhr上遇到错误事件?你能在那里提醒或记录一些东西吗?你认为这些错误会影响隐藏模式吗?不起作用。不知道问题是否与部分视图的使用有关。当我的ModelView无效时,将暂停部分视图的返回,因此调用函数erros。我使用$('.modal background').hide()修复了我的问题$(“#itemLoader”).modal('dispose');顺便说一下,谢谢你的帮助。正如你告诉我的,我用success函数修改了一些代码。
<button id="btnclosemodal" class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
// Does not work
$('#btnclosemodal').click();
// Does not work
$("#btnclosemodal").trigger("click");