如何异步呈现局部视图 [英] How to render a partial view asynchronously

查看:63
本文介绍了如何异步呈现局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分视图可以异步呈现吗?

Can a partial view be rendered asynchronously?

我有一个局部视图,需要呈现博客文章.博客帖子是异步返回的.

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

在我的_Layout文件中,渲染我的部分页脚_Footer.在_Footer中,我具有以下标记:

In my _Layout file I render my partial footer _Footer. In _Footer I have the following markup:

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

因此在我的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);
}

在我的FooterLatestBlogPosts部分视图中,我具有以下内容:

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>
}

我遇到错误:

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

我应该只创建一个同步方法来带回我的数据吗?

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

推荐答案

我在@buffjape建议的帖子中回答了这个问题:

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

异步PartialView会导致"HttpServerUtility.Execute被阻止...".例外

我将所有方法都更改为同步.

I changed my methods all to synchronous.

这篇关于如何异步呈现局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆