Organizing the user interface
In this section, we will explore three options:
- Partial views to encapsulate reusable UI parts.
- Tag Helpers, which enable us to write HTML-like Razor code instead of a C#-like syntax.
- View components, which allow encapsulating logic with one or more views to create reusable components.
Keep in mind that we can use these options in both MVC and Razor Pages.
Partial views
A partial view is a part of a view created in a cshtml
file, a Razor file. The content (markup) of the partial view is rendered at the location it was included by the <partial>
Tag Helper or the @
Html.PartialAsync()
method. ASP.NET introduced the concept in MVC, hence the view. For Razor Pages, you could see partial views as partial pages.
We can place partial view files almost anywhere in our projects, but I’d suggest keeping them close to the views that use them (in the same folder, for example). You can also keep them in the...