-
LoadingElementId
应该直接指向
.gif
形象
-
您的图像
src
src="~/photos/image/loading.gif"
-
最后,为了让AJAX调用在MVC中正常工作,您需要添加对三个javascript库的引用。请注意-
命令很重要
:
3.1)jquery-1.8.0.js
3.2)jquery.validate.js
完成下面的示例。
控制器:
public class HomeController : Controller
{
public string Index(string search)
{
Thread.Sleep(5000);
return "Hello " + search;
}
}
视图:
<script src="~/scripts/jquery-1.8.0.js"></script>
<script src="~/scripts/jquery.validate.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.js"></script>
@using (Ajax.BeginForm("Index", "Home", null, new AjaxOptions()
{
UpdateTargetId = "result",
LoadingElementId = "myLoadingElement"
},
null))
{
@Html.TextBox("search", null, new { style = "width:500px;" })
<input type="submit" value="search" />
}
<img id="myLoadingElement" src="~/photos/image/loading.gif" style="display:none;width:70px;height:70px;" />
<div id="result">
</div>
编辑:
Ajax.BeginForm
当您想调用控制器操作并在同一页面上显示结果时,使用。如果您想调用控制器操作,并在完成后重定向到其他视图,则应使用标准
Html.BeginForm
并使用jQuery显示加载的.gif:
<script src="~/scripts/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(function () {
$("#myform").submit(function (e) {
$("#myLoadingElement").show();
});
});
</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myform" }))
{
@Html.TextBox("search", null, new { style = "width:500px;" })
<input type="submit" value="search" />
}
<img id="myLoadingElement" src="~/photos/image/loading.gif" style="display:none;width:70px;height:70px;" />