How to render a partial view asynchronously

前端 未结 2 772
刺人心
刺人心 2021-02-01 16:11

Can a partial view be rendered asynchronously?

I have a partial view that needs to render blog posts. The blog posts are returned asynchronously.

In my _La

2条回答
  •  一个人的身影
    2021-02-01 17:05

    First of all you need to use Html.Partial as suggested by @buffjape. If your partial view is not in Shared folder you need to specify the path to the view

    @Html.Partial("~/Views/Common/FooterLatestBlogPosts", yourModel)

    However in this case your view is still loaded synchronously. To load it in async way you need to load it via jQuery. Article Improve perceived performance of ASP.NET MVC websites with asynchronous partial views gives a very good description on how to achieve it.

    Also replace your Html.Render with

    $(document).ready(function(){
         $("#yourContainer").load('@Url.Action("FooterLatestBlogPosts", "Common")')
    });
    

提交回复
热议问题