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:
Why do I get the text line in my html
How do I get rid of it and still be able to use RenderPartialAsync?
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.
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.
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.
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);}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With