简体   繁体   English

jQuery 加载新视图然后加载部分视图

[英]jQuery to load new view and then load partial view

I have a asp.net MVC view.我有一个 asp.net MVC 视图。 A button click on this view should redirect the user to another view.单击此视图的按钮应将用户重定向到另一个视图。 The second view is built with few partial views.第二个视图是用很少的局部视图构建的。 One of the partial view is taking long time to load, so i don't want this overhead on the page load and i want that partial view to run on ajax call, so that it can run asynchronously.其中一个局部视图需要很长时间才能加载,所以我不希望页面加载产生这种开销,我希望该局部视图在 ajax 调用上运行,以便它可以异步运行。 But on redirect, I want to return the second view with other partial views.但是在重定向时,我想用其他部分视图返回第二个视图。 Please suggest a workaround for this.请为此提出一个解决方法。

 $(document).on('click', '#buttonClick', function () {
    window.location = '/some/someAction?id=123';
     $.ajax ({
        type: 'GET',
        url: '/some/LoadPartialView?id=123',

        success: function(data){
             $('#timeTakingPartialView').html(data);

        }
    });

});

Window.location is redirecting to second view after the ajax call. Window.location 在 ajax 调用后重定向到第二个视图。 So reload is stepping over the ajax call.所以重新加载是跳过ajax调用。

If I understand you right, you want to load that partial view before call main view.如果我理解正确,您想在调用主视图之前加载该部分视图。 I don't think that is possible.我不认为这是可能的。 I would suggest adding this partial view "lazy" loading on the second view.我建议在第二个视图上添加这个局部视图“延迟”加载。 So for the first view you only need to add button click event:因此,对于第一个视图,您只需要添加按钮单击事件:

$(document).on('click', '#buttonClick', function () {
    window.location = '/some/someAction?id=123'; 
});

And when you locate to the second view (someAction), you able to load on a ready event when the DOM is loaded:当您定位到第二个视图 (someAction) 时,您可以在加载 DOM 时加载就绪事件:

$(document).ready(function () {
     $.ajax ({
        type: 'GET',
        url: '/some/LoadPartialView?id=123',

        success: function(data){
             $('#timeTakingPartialView').html(data);
        }
    });
});

So, the loading of a slow partial view will be separated.因此,缓慢的局部视图的加载将被分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM