尝试使用简单的 ASP.NET 核心网站进行 RenderPartialAsync 时出现编译器错误
Compiler error when trying to RenderPartialAsync with a simple ASP.NET Core website
总结
我想在尝试呈现 ASP.NET 核心部分视图时更正 use/calling 的 RenderPartialAsync
方法。
详情
我正在尝试在我的简单 ASP.NET Core 5.0 网站中呈现部分视图:
<td>
@await Html.RenderPartialAsync("_Listings", item.Listings);
</td>
当我尝试以下操作时,我不断收到编译器错误:
Cannot implicitly convert type 'void' to 'object'
我不明白它在抱怨什么。我 猜测 这个方法正在等待一个没有 return 任何东西的任务...但是它想要 returned?
我认为 RenderPartialAsync
method will render the contents to the response stream inside 那个方法...不是 return 一些 HTML 然后我需要做一些事情,与.
In this context, "renders" means the method writes its output using Writer.
我做错了什么?是我的剃须刀 'code snippet' start/end 代码块的位置还是什么?
此外,我尝试使用 Html.RenderPartial
(注意这是 SYNC 方法),但我收到了有关此方法如何阻止的警告以及相同的错误消息。
更新 #1
What is the return type of item.Listings
答案:IEnumerable<Listing>
所以我不得不更改代码:
<td>
@await Html.RenderPartialAsync("_Listings", item.Listings);
</td>
到
<td>
@{
await Html.RenderPartialAsync("_Listings", item.Listings);
}
</td>
我不明白为什么,但现在可以了。
我的 猜测 是 @await <stuff>
是两个命令,而不是一个 .. 所以它需要以不同的方式处理。而 @{ stuff }
是一个代码块 section.
但正确的答案应该是改用 Tag Helpers。
所以这样:
<td>
<partial name="_Listings" model="item.Listings" />
</td>
总结
我想在尝试呈现 ASP.NET 核心部分视图时更正 use/calling 的 RenderPartialAsync
方法。
详情
我正在尝试在我的简单 ASP.NET Core 5.0 网站中呈现部分视图:
<td>
@await Html.RenderPartialAsync("_Listings", item.Listings);
</td>
当我尝试以下操作时,我不断收到编译器错误:
Cannot implicitly convert type 'void' to 'object'
我不明白它在抱怨什么。我 猜测 这个方法正在等待一个没有 return 任何东西的任务...但是它想要 returned?
我认为 RenderPartialAsync
method will render the contents to the response stream inside 那个方法...不是 return 一些 HTML 然后我需要做一些事情,与.
In this context, "renders" means the method writes its output using Writer.
我做错了什么?是我的剃须刀 'code snippet' start/end 代码块的位置还是什么?
此外,我尝试使用 Html.RenderPartial
(注意这是 SYNC 方法),但我收到了有关此方法如何阻止的警告以及相同的错误消息。
更新 #1
What is the return type of
item.Listings
答案:IEnumerable<Listing>
所以我不得不更改代码:
<td>
@await Html.RenderPartialAsync("_Listings", item.Listings);
</td>
到
<td>
@{
await Html.RenderPartialAsync("_Listings", item.Listings);
}
</td>
我不明白为什么,但现在可以了。
我的 猜测 是 @await <stuff>
是两个命令,而不是一个 .. 所以它需要以不同的方式处理。而 @{ stuff }
是一个代码块 section.
但正确的答案应该是改用 Tag Helpers。
所以这样:
<td>
<partial name="_Listings" model="item.Listings" />
</td>