Ad

How To Divide HTML Found In .cshtml Files

- 1 answer

I've got an ASP.NET Core MVC Web App, and one of my Views has too much HTML, causing it to look messy and difficult to find stuff, is there a way to extract some of the HTML (for example a certain div) to a different .cshtml file that I can then include in the View?

Ad

Answer

Here is a partial view sample:

Shared/partial1.cshtml:

<h1>partial1</h1>

Test.cshtml:

<partial name="partial1" />

Or:

@await Html.PartialAsync("partial1.cshtml")

result:

enter image description here

You can also use View Component,here is an official doc.

Ad
source: stackoverflow.com
Ad