Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

Blazor Forms and Immutability: Keep a Stable Draft, Update State on Submit

Blazor’s EditContext tracks field changes and validation for a model instance. Keep a stable draft while editing, then create immutable application state on valid submission.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Blazor forms can work with immutable application state, but the object being edited should usually stay stable for the duration of the form session. EditContext tracks modified fields and validation messages for that session; replacing the form’s model with a new object creates a new context and clears that tracked state. A practical approach is to edit a stable draft, then create a new immutable state value when the form is valid and submitted.

Why replacing a form model can reset its state

An EditForm accepts either a model or an EditContext. The context holds form-session metadata, including which fields have changed and the current validation messages. As Microsoft’s forms-binding documentation puts it, “When a different object instance is assigned to the Model parameter of EditForm, the form creates a new EditContext.” The new context starts without the previous context’s modified-field and validation-message state.

As an Amazon Associate I earn from qualifying purchases.

This matters when application updates use immutable replacement: rather than changing an object in place, code creates a new object and makes it the current state. That approach can be suitable for application state, but replacing the model while a form is being edited also replaces the form’s editing session. Depending on when and why the replacement happens, the UI may no longer show the previous modified-field tracking or validation messages.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose where immutability belongs

Keep the object used for editing stable while the user works, and treat it as a draft. When validation succeeds, map its values into a newly constructed immutable application-state value or dispatch an action that does so. This separates two responsibilities: the draft supports Blazor’s form-editing and validation lifecycle; the submitted state can follow an immutable update pattern.

This is an architectural recommendation inferred from the documented EditContext behavior, not a Microsoft rule requiring drafts or prohibiting immutable models. The right boundary depends on the application’s state-management design.

Approach EditContext tracking Fit with immutable application state Implementation trade-off
Keep a stable draft model during editing Preserves the same editing session while fields and validation are tracked. Build a new immutable state value from the draft after a valid submission. Requires a draft-to-state mapping step.
Replace the form model while editing A different model instance creates a new EditContext, whose modified-field and validation-message state starts empty. Directly follows replacement-style updates, but can interrupt the active form session. Requires deliberate management of the new context and any form metadata the application needs to preserve.

Bind inputs to the draft and submit only valid values

Standard input components bind to properties on the form model. The following example keeps one draft instance for the component’s lifetime and creates a separate immutable value when the form is valid:

@using System.ComponentModel.DataAnnotations

<EditForm Model="draft" OnValidSubmit="Save">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputText @bind-Value="draft.Name" />
    <ValidationMessage For="() => draft.Name" />

    <button type="submit">Save</button>
</EditForm>

@code {
    private readonly ProfileDraft draft = new();
    private ProfileState? savedState;

    private void Save()
    {
        savedState = new ProfileState(draft.Name);
    }

    private sealed class ProfileDraft
    {
        [Required]
        public string Name { get; set; } = "";
    }

    private sealed record ProfileState(string Name);
}

OnValidSubmit runs the handler only when the form is valid. In a state-managed application, the handler can instead dispatch the new value to the state layer. The key is that ordinary typing updates the draft’s bound property rather than replacing the model instance on each change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep child parameters immutable while supporting binding

A child component should generally avoid assigning directly to a parameter it received from its parent. Microsoft’s .NET 10 data-binding documentation explains: “The reason to avoid directly changing the value of a component parameter is that it effectively mutates the parent’s state from the child component.” For two-way binding, the documented @bind:get and @bind:set modifiers allow the child to keep its parameter immutable while routing changes through the parent. Microsoft describes the goal as: “Keep the parameter immutable in the child, while still supporting two-way binding.”

For example, a parent can bind a value and handle the requested update:

<NameEditor @bind-Value:get="currentName"
            @bind-Value:set="SetName" />

@code {
    private string currentName = "Ada";

    private void SetName(string nextName)
    {
        currentName = nextName;
    }
}

The child exposes a parameter and matching callback, then uses the binding pair on its own input:

<InputText @bind-Value="Value" />

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

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }
}

In production, this child input should be part of an EditForm when it needs the form’s validation context. Use the binding modifiers to send changes back through the parent’s update path instead of mutating a received parameter as though it were locally owned state.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the right base for a custom input

For a custom control that participates in an EditForm, Microsoft recommends deriving from InputBase<TValue> unless a requirement prevents it. The base class supplies common binding, change callbacks, and validation integration, and it must be used inside an EditForm. A fully custom input can provide more control, but its implementation must handle the input-processing responsibilities itself.

Custom-input approach What it provides What you take on
Derive from InputBase<TValue> Common binding, callbacks, and validation behavior. Use the component within an EditForm and implement the specialized rendering or behavior.
Implement input processing fully yourself Control over the complete input-processing implementation. Binding, callbacks, and validation behavior must be implemented by the component.

When replacing the model is intentional

Sometimes an application deliberately changes the model during editing. In that case, treat the new model as a new form session: manage the EditContext deliberately and decide whether the loss of its previous field and validation metadata is acceptable. If that metadata must be retained, the design needs to preserve or restore it explicitly; simply assigning a different model instance does not carry it across automatically.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.