C# Ajax.BeginForm 中的 LoadingElementId 不显示加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10820042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LoadingElementId in Ajax.BeginForm not displaying the loading image
提问by BoundForGlory
I have an MVC3 app, and i need to add ajax loading image. I put this on my view
我有一个 MVC3 应用程序,我需要添加 ajax 加载图像。我把这个放在我的观点上
@using (Ajax.BeginForm(
"LogOn","Account",
new AjaxOptions { LoadingElementId = "div_loading" }))
This is my very simple div on the same view:
这是我在同一视图上的非常简单的 div:
<div id="div_loading" style="display:none;">
<img src="@Url.Content("~/Content/Images/loading.gif")" alt="" />
</div>
When i click my submit button to post the form back, the loading.gif never appears. My code runs fine, but no loading indicator shows up. And the image is named correctly and in the right place. Does anyone have a clue why?
当我单击提交按钮将表单发回时,loading.gif 永远不会出现。我的代码运行良好,但没有显示加载指示器。并且图像被正确命名并位于正确的位置。有谁知道为什么?
UpdateApparently, this has something to do with Telerik applications. If i create a regular MVC3 app, it works. When i create a Telerik MVC3 app, it doesnt work. Has anyone else encountered this with Telerik?
更新显然,这与 Telerik 应用程序有关。如果我创建一个常规的 MVC3 应用程序,它就可以工作。当我创建 Telerik MVC3 应用程序时,它不起作用。有没有其他人在 Telerik 上遇到过这个问题?
回答by Totero
I prefer to shy away from attaching a loading iamge to every ajax call I make.
我更喜欢避免将加载图像附加到我所做的每个 ajax 调用中。
The top answer here is a solution to display an ajax spinner (loading image) whenever ajax makes a send call:
这里的最佳答案是在 ajax 发出发送调用时显示 ajax 微调器(加载图像)的解决方案:
Display spinner while waiting For ajax
It has the functionality you need. Just use javascript to attach the display of the image to the ajax calls.
它具有您需要的功能。只需使用 javascript 将图像的显示附加到 ajax 调用。
It should be something like:
它应该是这样的:
<script type="text/javascript">
$(document).ready(function () {
BindSpinner();
});
function BindSpinner() {
$("#div_loading").bind("ajaxSend", function () {
$(this).show();
}).bind("ajaxStop", function () {
$(this).hide();
}).bind("ajaxError", function () {
$(this).hide();
});
};
</script>
What this does is display the div on ajax send and hides it upon ajax stop or error for all calls on your page. If you place this script and the div in your _layout it will do this for every Ajax call on your site.
这样做是在 ajax 发送时显示 div,并在页面上的所有调用的 ajax 停止或错误时隐藏它。如果您将此脚本和 div 放在您的 _layout 中,它将为您网站上的每个 Ajax 调用执行此操作。
回答by Jason Kulatunga
回答by Jason Kulatunga
You could try this as well from: here
function AjaxBegin()
{
$('#div_loading').show();
}
function AjaxComplete()
{
$("#div_loading").hide();
}
function AjaxFailure(ajaxContext)
{
var response = ajaxContext.responseText;
alert("Error Code [" + ajaxContext.ErrorCode + "] " + response);
}
AjaxOptions:
OnFailure = "AjaxFailure";
OnBegin = "AjaxBegin";
OnComplete = "AjaxComplete";
回答by Flavio Rodrigues
I had the same issue and was able to solve it changing the name of the element to be hidden/shown. I used camel-case format: "myDivToBeHidden"
我遇到了同样的问题,并且能够通过更改要隐藏/显示的元素的名称来解决它。我使用驼峰格式:“myDivToBeHidden”
The element needs the "display" property set to "none".
该元素需要将“display”属性设置为“none”。

