liveBook · Manning

Attribute splatting

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

Attribute splatting is the technique of applying a collection of attribute names and values to an element in one operation.

Attribute splatting applies a collection of attribute names and values to an element in one operation. In a Blazor component, a parameter marked with CaptureUnmatchedValues receives attributes that do not match named parameters, and @attributes forwards them to the rendered element. section 34.2.1

Implement attribute splatting by:

  • Declaring a Dictionary<string, object> parameter with [Parameter(CaptureUnmatchedValues = true)]. section 34.2.1

Forwarding attributes through SelectFilter

A SelectFilter component can accept values and a title while forwarding arbitrary HTML attributes to its select element. Use <SelectFilter values="@Cities" title="City" autofocus="true" name="city" required="true" />; the rendered select receives autofocus, name, and required. Inspect the page at /controllers to verify the generated attributes. section 34.2.1

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

How deep it goes
Explains the catchall dictionary, @attributes syntax, and rendered result, but does not detail attribute precedence or conflicts.
Examples
Uses SelectFilter with autofocus, name, and required attributes, plus InputNumber with a disabled attribute.
What it assumes
Assumes familiarity with Blazor components and parameters while demonstrating the required dictionary and Razor syntax.
What you can do after
Build it from scratch by adding a catchall parameter and forwarding its dictionary to a rendered element.
How it connects
Connects Blazor components, component parameters, HTML attributes, and Blazor form components.

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.