liveBook · Manning

Partial element

As explained in Pro ASP.NET Core 7, Tenth Edition

A partial element is an ASP.NET Core HTML-like element that invokes a partial view and supplies its view model.

A partial element invokes a Razor partial view and supplies its view model. Use it to move repeated markup into one template while leaving the rendered response unchanged. In MVC views, place it directly in a loop or another view; in Razor Pages, the partial receives the selected model value rather than the page model. section 22.5.3 section 23.5.2 section 7.5.3

Configure the element with attributes that control lookup and model data:

  • Use name to select the partial view; lookup follows the regular view-search process. section 22.5.3
  • Use model to pass an explicit object, such as p in a product loop or Model.Product in a Razor Page. section 22.5.3 section 23.5.2
  • Use for to select a model value through an expression, such as a Product object's Name property. section 22.5.3

Render product rows

A strongly typed List.cshtml view can iterate over products and invoke _RowPartial for each one: <partial name="_RowPartial" model="p" />. The row partial can then invoke _CellPartial with for="Name", passing the product's string property to a separately typed cell template. Requesting /home/list renders the resulting table.

Share a product summary

Define ProductSummary.cshtml in Views/Shared with @model Product, then invoke it from Index.cshtml using <partial name="ProductSummary" model="p" />. The same product-card markup can appear in any view that needs the summary.

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains invocation, lookup, model selection, nested partials, and rendered results, but defers templated delegates as an awkward alternative.
Examples
Demonstrates product tables, nested name cells, Razor Page product details, and reusable SportsStore product cards.
What it assumes
Assumes Razor view models and partial-view files, while demonstrating model typing and view lookup through complete application paths.
What you can do after
Apply it via Razor markup to compose reusable partial views and pass either complete models or selected model properties.
How it connects
Connects partial views with Razor views, Razor Pages, view models, tag helpers, layouts, and templated delegates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.