Search code examples
c#asp.net-corerazor

Adding await to RenderPartialView produces an error


This is an ASP.net core web application view. This works:

        @if (r.Identifier.HasAcronym)
        {
            @Html.RenderPartialAsync("Resources", new Tuple<IEnumerable<FileSystemObject>, string>(r.ResourcesByAcronym, r.Identifier.Acronym));
        }

this produces the error: Cannot implicitly convert type 'void' to 'object'

        @if (r.Identifier.HasAcronym)
        {
            @await Html.RenderPartialAsync("Resources", new Tuple<IEnumerable<FileSystemObject>, string>(r.ResourcesByAcronym, r.Identifier.Acronym));
        }

Solution

  • You can write like this:

    <td>
        @{
            await Html.RenderPartialAsync("Resources", new Tuple......);
        }
    </td>
    

    Html.RenderPartialAsync returns a Task, so the result type is void. The @ without braces asks Razor to render the result, which is not possible: There is no way to convert void to anything renderable. This would only be the right way to go if Html.RenderPartialAsync returned a string, which needed to be rendered manually. The braces introduce a new code block, which allows standard C# syntax.