我有这个简单的MVC 3应用程序,它只有两个控制器(Home和Edit),每个控制器有两个动作,每个动作有两个视图,
每个视图都有一个ajax动作链接,它只是请求另一个视图并在div中显示结果,问题是在一定数量的请求后页面变得越来越慢,我用fiddler检查了它并注意到了每次按动作链接时,请求将根据等式(2到幂n)发送多次,其中n是按下链接的时间,在控制器和视图下方找到。
控制器
public class EditController : Controller
{
public ActionResult Index()
{
return View();
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Text = "Home Page";
return View();
}
}
和观点
主页视图
@{
ViewBag.Title = "Home";
}
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script type="text/javascript">
function myCallback(xhr, status) {
alert(status);
}
</script>
<div id="divLoading" style="display: none">
Loading ...</div>
<div id="divResultText">
@Ajax.ActionLink("Edit Ajax", "Index", "Edit", null, new AjaxOptions
{
HttpMethod = "Get",
OnComplete = "myCallback",
UpdateTargetId = "divResultText",
LoadingElementId = "divLoading"
})
</div>
修改视图
@{
ViewBag.Title = "Edit";
}
@Ajax.ActionLink("Home", "Index", "Home", null, new AjaxOptions
{
HttpMethod = "Get",
UpdateTargetId = "divResultText",
LoadingElementId = "divLoading"
})