Alex Chadwick
Alex Chadwick

Reputation: 76

How to divide HTML found in .cshtml files

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?

Upvotes: 0

Views: 244

Answers (1)

Yiyi You
Yiyi You

Reputation: 18189

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.

Upvotes: 1

Related Questions