Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a partial view asynchronously

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 _Layout file I render my partial footer _Footer. In _Footer I have the following markup:

@Html.Action("FooterLatestBlogPosts", "Common") 

So in my Common controller I have the following action method:

public async Task<ActionResult> FooterLatestBlogPosts() {      List<ArticleDTO> articleDTOs = await articleTask.GetAllAsync();       return PartialView(articleDTOs); } 

In my FooterLatestBlogPosts partial view I have the following:

@model List<MyProject.Application.DTO.ArticleDTO> @if (Model.Count > 0) {      <ul class="list-unstyled">           @foreach (var articleDTO in Model)           {                <li>@articleDTO.Title</li>           }      </ul> } 

I'm getting an error:

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper' 

Should I rather just create a synchronous mthod to bring back my data?

like image 311
Brendan Vogt Avatar asked Nov 25 '15 10:11

Brendan Vogt


People also ask

How do I render a partial part of a view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

How do I return a partial view to another controller?

Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder. And then call it from controller as mentioned above. That's it.

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.

How do you call a partial view inside another partial view?

To call a partial view from another partial view, we follow the same approach as we follow when we call partial view from the content page. Let's take an example. This is the code of 1st partial view. Notice that in above partial view, we are calling another partial view named "_View2" that is created below.


2 Answers

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")') }); 
like image 141
Andrei Mihalciuc Avatar answered Sep 17 '22 15:09

Andrei Mihalciuc


I went with the answer in the post that @buffjape suggested:

Async PartialView causes "HttpServerUtility.Execute blocked..." exception

I changed my methods all to synchronous.

like image 26
Brendan Vogt Avatar answered Sep 16 '22 15:09

Brendan Vogt