我的MVC应用程序中有一个布局页面,在此布局页面上的menü链接的帮助下,我渲染了一个只有5-6输入而没有css或js导入的PartialView。因为它可以在下面的图像中看到,只需加载此PartialView大约需要4秒钟,我认为这个逻辑存在很大的错误。你能不能看一下这个页面并澄清我错误的地方?
注意:据我所知,AJAx方法的耗时部分是成功部分(直到$(“#div-page-content”)。html('');线)。是否需要或如何使用更快的代码块更改该部分?
LayoutPage:
<a href="#" id="register" class="nav-link" onclick="renderPartial(event, 'Account', 'Create')">
<div id="div-page-content" class="page-content">
@RenderBody()
</div>
<script>
function renderPartial(e, controller, action) {
e.preventDefault();
var controllerName = controller;
var actionName = action;
if (String(actionName).trim() == '') {
return false;
}
if (typeof (controllerName) == "undefined") {
return false;
}
var url = "/" + controllerName + "/" + actionName;
$.ajax({
url: url,
data: { /* additional parameters */ },
cache: false,
type: "POST",
dataType: "html",
success: function (data) {
var requestedUrl = String(this.url).replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
if (typeof (requestedUrl) == "undefined" || requestedUrl == 'undefined') {
requestedUrl = window.location.href;
}
// if the url is the same, replace the state
if (typeof (history.pushState) != "undefined") {
if (window.location.href == requestedUrl) {
history.replaceState({ html: '' }, document.title, requestedUrl);
}
else {
history.pushState({ html: '' }, document.title, requestedUrl);
}
}
$("#div-page-content").html('');
$("#div-page-content").append(data);
},
error: function (data) { onError(data); }
});
};
</script>
_PartialView:
<div>
//... code omitted fr borevity (there is no js or css import in this view
</div>
控制器:
[HttpPost]
public ActionResult Create()
{
return PartialView("_Register");
}