Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I change @Html.Partial to @Html.PartialAsync as Visual Studio suggest?

In my code I have @Html.Partial("_StatusMessage", Model.StatusMessage) but Visual Studio warning me that: Error MVC1000: Use of IHtmlHelper.Partial may result in application deadlocks. Consider using <partial> Tag Helper or IHtmlHelper.PartialAsync.

Should I disable this error or I should really change @Html.Partial to @Html.PartialAsync, and why?

like image 446
DiPix Avatar asked Jun 09 '18 13:06

DiPix


People also ask

What is difference between HTML partial and HTML RenderPartial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

What is HTML PartialAsync?

Returns HTML markup for the specified partial view. PartialAsync(IHtmlHelper, String, ViewDataDictionary) Returns HTML markup for the specified partial view.

When should partial views be used?

Partial views are an effective way to: Break up large markup files into smaller components. In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view.

What option does Visual Studio provides to create a view as a partial view?

Creating Partial View To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view.


1 Answers

Yes we should, See below section from their official site

Migrate from an HTML Helper

Consider the following asynchronous HTML Helper example. A collection of products is iterated and displayed. Per the PartialAsync method's first parameter, the _ProductPartial.cshtml partial view is loaded. An instance of the Product model is passed to the partial view for binding.

CSHTML      @foreach (var product in Model.Products)     {         @await Html.PartialAsync("_ProductPartial", product)     } 

The following Partial Tag Helper achieves the same asynchronous rendering behavior as the PartialAsync HTML Helper. The model attribute is assigned a Product model instance for binding to the partial view.

CSHTML  @foreach (var product in Model.Products) {     <partial name="_ProductPartial" model="product" /> }  

Copied from https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.1

like image 106
MrNams Avatar answered Oct 07 '22 23:10

MrNams