简体   繁体   English

主视图后加载局部视图

[英]Load partial view after main view

I have several partial views that have been added to my main view on my MVC site. 我在MVC网站的主视图中添加了一些局部视图。 One of the partial views takes a long time to load however, as it can take a while to retrieve the data for it. 但是,部分视图之一需要很长时间才能加载,因为要为其检索数据可能需要一段时间。 Is it possible to load the main view and start loading the partial views, but if the view is still loading then display the rest in the meantime, with the final one showing once it has finished? 是否可以加载主视图并开始加载局部视图,但是如果视图仍在加载,则同时显示其余视图,并在完成后显示最后一个视图?

On a similar note, while a partial view is loading (or being refreshed) how can I get some kind of "Loading" screen to show over the view area so that the user knows that something is happening? 类似地,当部分视图正在加载(或刷新)时,如何获得某种“正在加载”屏幕以显示在视图区域上,以便用户知道正在发生什么?

Thanks 谢谢

Try the below codes 试试下面的代码

$.ajax(
          {
              url: '/Controller/Action',
              data: { Id: '1' }, //input parameters to action
              beforeSend: function () {
                  $('#div-result').show();
                  var img = '<img src="../../Images/loading.ico" />';
                  $('#div-result').html(img);
              },
              success: function (data) {

                  // $('#div-result') -> div in the main view

                  $('#div-result').html(data);
              }
          });

As Alexey says, simply add content element(s) where you need the partial views 正如Alexey所说,只需在需要局部视图的地方添加内容元素

<div id="partialViewHolder" style="display:none">Ajax content goes here after initial page loads, but it is hidden until then</div>

And then in your javascript, add something like (assuming you have already loaded jquery into the DOM): 然后在您的JavaScript中,添加类似的内容(假设您已经将jQuery加载到DOM中):

<script type="text/javascript">
$(window).load(function () {
   $( "#partialViewHolder" ).load( "@Url.Content("Your PartialView Controller Method Goes Here")" );
   $( "#partialViewHolder" ).show();
}
</script>

Note that you need to use $(window).load() instead of $(document).ready() as a trigger to allow the page to render while the partial view is loading. 请注意,您需要使用$(window).load()代替$(document).ready()作为触发器,以允许页面在部分视图加载时呈现。

As I understand, you want to get partial view async. 据我了解,您希望获得部分视图异步。 You can use Ajax to draw partial view, for example, by using jQuery ajax to get data from server and draw it, when ajax request is done. 您可以使用Ajax绘制部分视图,例如,当ajax请求完成时,通过使用jQuery ajax从服务器获取数据并绘制数据。 So your main view will be loading much faster. 因此,您的主视图将加载得更快。

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

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