Blazor · Components

Data Binding

Data binding keeps a C# field and an input in sync in both directions with a single attribute.

Intermediate 5 min Lesson 10 of 31Examples target .NET 9 Blazor Web App.
Before you start:Event Handling
0/31 in Blazor
On this page · 5 min · Intermediate

Learning objectives#

  • Bind inputs with @bind
  • Choose when binding updates with @bind:event
  • Format values and run code after binding
  • Create bindable component parameters

Explanation#

@bind="field" sets the input’s value from the field and updates the field when the input changes.

By default it updates on change (when the input loses focus). @bind:event="oninput" updates on every keystroke.

@bind:format formats dates; @bind:after runs a method after the value is updated — handy for search-as-you-type.

A component can support @bind-Value by exposing Value and ValueChanged (EventCallback<T>) parameters.

Code example: Live product filter#

Razor / MarkupComponents/Pages/Filter.razor Illustrative
@page "/filter"
@rendermode InteractiveServer


<input @bind="search" @bind:event="oninput" @bind:after="Apply" placeholder="Search…" />
<select @bind="maxPrice" @bind:after="Apply">
    <option value="100000">Any price</option>
    <option value="5000">Under ₹5,000</option>
</select>
<label><input type="checkbox" @bind="inStockOnly" @bind:after="Apply" /> In stock only</label>


<p>@results.Count result(s)</p>


@code {
    private string search = "";
    private int maxPrice = 100000;
    private bool inStockOnly;
    private List<Product> all = Product.Samples, results = Product.Samples;


    private void Apply() => results = all
        .Where(p => p.Name.Contains(search, StringComparison.OrdinalIgnoreCase))
        .Where(p => p.Price <= maxPrice && (!inStockOnly || p.Stock > 0))
        .ToList();
}
Expected output
Typing “mo” immediately shows “1 result(s)” (Mouse). Changing the price or ticking “In stock only” re-filters at once.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. @bind:event="oninput" updates search on every keystroke.
  2. @bind:after calls Apply after each value change.
  3. select and checkbox bindings convert to int and bool automatically.
  4. Apply recomputes the results list, and the count re-renders.

Real-world use case#

Admin tables with instant filters, settings forms and calculators that update totals as the user types.

Common mistakes#

Using both @bind and @onchange on one element

@bind already uses onchange. Use @bind:after to react to changes.

Binding expensive filters to every keystroke on huge lists

Debounce, or filter on the server with paging.

Best practices#

  • Use @bind:after for side effects
  • Use oninput only when you need live updates
  • Format dates with @bind:format
  • Expose Value/ValueChanged for reusable inputs

Remember this#

@bind = value in + changes out.

Default updates on change; oninput = every keystroke.

@bind:after = run code after the update.

Practice exercise#

Your task: Bind a DateOnly field to a date input showing yyyy-MM-dd.

Show solution
Razor / Markup Illustrative
<input type="date" @bind="start" @bind:format="yyyy-MM-dd" />


@code { private DateOnly start = DateOnly.FromDateTime(DateTime.Today); }

Mini quiz#

Data Binding quizMultiple choice Question 1 of 1

How do you make @bind update on every keystroke?

Score so far: 0/0

Want more? Practise all Blazor MCQs · Blazor interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.