The Partial Tag Helper

The Partial tag helper is designed to replace the Html.Partial and Html.RenderPartial methods as the recommended mechanism for including partial pages in a Razor content page.

Attribute Description
name The name of the partial file
model The model to be passed in to the partial view
for An expression to be evaluated against the current page model. Cannot be used in conjunction with the model attribute
view-data A ViewData dictionary to be passed to the partial view

Notes

The partial tag helper will always load the partial content asynchronously.

The name attribute will search all registered partial locations for a file with the supplied name (without the extension). The following example will search for _MyPartial.cshtml in Pages, Pages/Shared and Views/Shared by default:

<partial name="_MyPartial" ... />

You can provide a partial path, which will be appended to the search locations:

<partial name="Folder1/_MyPartial" ... />

Now the framework will search for the following:

  • Pages/Folder1/_MyPartial.cshtml
  • Pages/Shared/Folder1/_MyPartial.cshtml
  • Views/Shared/Folder1/_MyPartial.cshtml

The model attribute and the for attribute both provide a means to pass data to the Partial. You can use one or the other but not both. The examples that follow illustrating the difference both assume that the current PageModel has a Contacts property:

<partial name="_MyPartial" model="Model.Contacts" />
<partial name="_MyPartial" for="@Model.Contacts" />
or
<partial name="_MyPartial" for="Contacts" />

The last two examples are equivalent. The @Model part of the expression is inferred in the second approach.

Last updated: 1/31/2021 5:25:36 PM

© 2018 - 2024 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com