Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderPartialAsync returns System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]

I am using ASP.NET 5 RC1

_MyPartial

@model MyViewModel  <div class="panel panel-primary">     <div class="panel-body">          @using (Html.BeginForm())             {             @Html.AntiForgeryToken()              <div class="form-horizontal">                 @Html.ValidationSummary(true)                 @Html.MyHtmlHelperRow(model => model.ShortDistrictName)                 @Html.MyHtmlHelperRowSaveButton(model => model.Id)             </div>         }     </div> </div> <div>     @Html.ActionLink("Back to List", "Index") </div> 

cshtml View

@model MyViewModel  @{     ViewBag.Title = "Edit"; }  <h2>@ViewBag.Title</h2>   @Html.RenderPartialAsync("_MyPartial", Model)  @section Scripts {     @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } } 

the line

@Html.RenderPartialAsync("_MyPartial", Model) 

apart from it rendering in html, renders

 System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult] 

text in the html directly below the last content rendered by the partial page.

If I change the partial line reference to non async

@Html.Partial("_MyPartial", Model) 

then I don't get this text line.

Questions:

  1. Why do I get the text line in my html

  2. How do I get rid of it and still be able to use RenderPartialAsync?

like image 258
zoaz Avatar asked Nov 24 '15 11:11

zoaz


People also ask

What is using system Threading tasks in C#?

Provides types that simplify the work of writing concurrent and asynchronous code. The main types are Task which represents an asynchronous operation that can be waited on and cancelled, and Task<TResult>, which is a task that can return a value.

What is HTML RenderPartialAsync?

the line @Html.RenderPartialAsync("_MyPartial", Model) apart from it rendering in html, renders System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult] text in the html directly below the last content rendered by the partial page.

Which Threading Task method will create a Task that will complete when any of the associated tasks have completed?

Wait method. A call to the Wait method blocks the calling thread until the single class instance has completed execution. The following example calls the parameterless Wait() method to wait unconditionally until a task completes. The task simulates work by calling the Thread.


2 Answers

You need to use await to call asynchronous methods. ASP.NET 5 adds the ability to use await in Razor:

@{await Html.RenderPartialAsync("_MyPartial", Model);} 
like image 156
Stephen Cleary Avatar answered Sep 23 '22 11:09

Stephen Cleary


using the syntax in the accepted answer gave me this error:

Argument 1: cannot convert from 'void' to 'object'

@await Html.RenderPartialAsync("_ProductList", Model)

in RC1 I had to use curly braces and a semicolon:

@{await Html.RenderPartialAsync("_ProductList", Model);} 

RenderPartialAsync writes directly to the response and returns void (technically Task), which the view compiler can't print as a string.

like image 45
Dave Thieben Avatar answered Sep 22 '22 11:09

Dave Thieben