Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 41 ยท Components and Razor Syntax

Component Parameters

Pass data into Blazor components with [Parameter]: required parameters, defaults, passing C# expressions, CaptureUnmatchedValues, route parameters, and why parameters should not be changed from inside.

Components become useful when you can configure them. A Button that always says "Click" is boring; a Button that takes a label, a colour and a size is reusable. In Blazor you pass data into a component with parameters: public properties marked with [Parameter]. The parent sets them with HTML-like attributes.

Declaring and setting parameters

HTML
@* Components/ProductCard.razor *@
<div class="card">
    <h3>@Title</h3>
    <p>@Price.ToString("C")</p>
    @if (InStock)
    {
        <span class="badge">In stock</span>
    }
</div>

@code {
    [Parameter] public string Title { get; set; } = "Untitled";
    [Parameter] public decimal Price { get; set; }
    [Parameter] public bool InStock { get; set; } = true;
}

The parent writes:

HTML
<ProductCard Title="Wireless mouse" Price="24.90m" InStock="true" />
<ProductCard Title="Keyboard" Price="@keyboardPrice" InStock="@(stock > 0)" />

Rules to remember:

  • A parameter is a public property with [Parameter], using { get; set; }.
  • A plain attribute value is a string, converted to the property type when it can be (Price="24.9", InStock="true").
  • Use @ to pass a C# expression: Price="@keyboardPrice" or InStock="@(stock > 0)". For non-string types, Blazor also lets you drop the @ when the value is a single identifier, but being explicit is clearer.
  • Unset parameters keep their default values, so give properties sensible defaults.

Required parameters

A parameter the component cannot work without should be marked [EditorRequired]:

C#
[Parameter, EditorRequired] public string Title { get; set; } = default!;

If a parent forgets Title, the compiler warns in the editor. With C# 11 you can also use required, but [EditorRequired] is the usual Blazor choice because it only produces a warning and works with the [Parameter] attribute.

Passing objects and collections

Parameters can be any type, including your own classes and lists:

HTML
<OrderSummary Order="currentOrder" Lines="@currentOrder.Lines" />

Be aware that passing a reference means both components see the same object. If the child mutates it, the parent's data changes too. When in doubt, pass immutable records or copies.

Never change parameters from inside

A parameter's value belongs to the parent. When the parent re-renders, Blazor sets the parameter again and overwrites anything the child assigned. Don't write Title = "new" in the child and expect it to stick. Instead:

  1. Copy the parameter into a private field when you need a changeable value, or
  2. Ask the parent to change it, using an EventCallback (Event Callbacks), or
  3. Use two-way binding (Data Binding).

Capturing extra attributes

What if someone writes <ProductCard class="wide" data-test="card1" /> and you didn't declare those? Collect them with CaptureUnmatchedValues:

HTML
<div @attributes="Extra" class="card">@Title</div>

@code {
    [Parameter] public string Title { get; set; } = "";

    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object>? Extra { get; set; }
}

@attributes splats the dictionary onto the element. This is how wrapper components, such as a custom Button, forward disabled, aria-*, id and class without declaring each one. Only one parameter per component can capture unmatched values.

Route parameters

For pages, parameters can come from the URL. They need @page with a placeholder and a matching property:

HTML
@page "/product/{Id:int}"

<h1>Product #@Id</h1>

@code {
    [Parameter] public int Id { get; set; }
}

Route constraints like :int are covered in chapter 5. Query string values use [SupplyParameterFromQuery] instead.

A runnable stand-in: how parameters are applied

The framework finds [Parameter] properties by reflection and assigns values from a name/value set. This stand-in does the same with our own attribute: it converts strings to the property type, reports missing required values and gathers the rest like CaptureUnmatchedValues.

C# Stand-in: reflection sets parameters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The values arrive as strings and the "framework" converts them, which mirrors why Price="24.90" works in markup. The unmatched class and data-test entries end up in Extra.

Common mistakes

  • Forgetting { get; set; }. A field or a read-only property cannot be a parameter.
  • Making the property private. Parameters must be public.
  • Mutating a list passed in. The parent's list changes too; copy first if you must modify it.
  • Treating a parameter as local state. Copy it into a field if the child needs to edit it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a property a component parameter?

    Parameters must be public properties marked with the [Parameter] attribute.
  2. 2. How do you pass the result of a C# expression to a parameter?

    Prefix the value with @ to evaluate C#; otherwise it is treated as a literal string.
  3. 3. Why shouldn't a child component assign to its own parameter properties?

    The parent owns the value and sets it again whenever it renders.
  4. 4. What does CaptureUnmatchedValues = true do?

    The dictionary can be splatted onto an element with @attributes, which is how wrapper components forward attributes.

Summary

  • Parameters are public [Parameter] properties set by the parent through attributes.
  • Use @ for expressions, [EditorRequired] for mandatory values and defaults for optional ones.
  • Do not modify parameters inside the child; copy them, or send changes up with callbacks or binding.
  • CaptureUnmatchedValues plus @attributes forwards extra HTML attributes.
  • Route values arrive as parameters on pages with @page placeholders.

Next: Child Content and Render Fragments