skip to content

A statically rendered Blazor Web App page posts a volunteer sign-up EditForm and gets a 400 or loses the typed values; how do FormName and [SupplyParameterFromForm] fix it?

level: seniorimportance: should knowfreq 48%

answer

  1. a POST must say which form
  2. FormName becomes the named submit event
  3. [SupplyParameterFromForm] maps posted fields
  4. Input names come from the bind expression
  5. Volunteer ??= new() in OnInitialized

basics

~20 s

Under static SSR each submit is a new HTTP POST to the page. FormName tells Blazor which form's handler to run, and [SupplyParameterFromForm] maps the posted fields onto the model property; without them the post is rejected or the values are lost.

solid answer

~40 s

A page with no render mode is rendered per request, so the submit is a plain HTTP POST that rebuilds the component. Blazor dispatches it by name: `EditForm` registers its `FormName` as a named submit event, and a post that names no form gets a 400 (with the message "The POST request does not specify which form is being submitted" shown only in Development); two forms with the same name in one scope throw instead. The posted values reach the model only through a property marked `[SupplyParameterFromForm]`; Input components generate field names such as `Volunteer.Email` from their `@bind-Value` expression so the mapping lines up. Because `OnInitialized` runs on the GET and again on every POST, initialize with `Volunteer ??= new()`, not `Volunteer = new()`, or the posted data is overwritten.

code

razor · 20 lines
razor
@page "/volunteer"
@inject VolunteerService Volunteers

<EditForm Model="Volunteer" FormName="volunteer-signup" OnValidSubmit="RegisterAsync">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <InputText @bind-Value="Volunteer!.FullName" />
    <InputText @bind-Value="Volunteer!.Email" />
    <InputNumber @bind-Value="Volunteer!.HoursPerWeek" />
    <button type="submit">Sign up</button>
</EditForm>

@code {
    [SupplyParameterFromForm]
    private VolunteerForm? Volunteer { get; set; }

    protected override void OnInitialized() => Volunteer ??= new();

    private Task RegisterAsync() => Volunteers.RegisterAsync(Volunteer!);
}

go deeper

for a junior

Recall that a static Blazor form needs a unique FormName and a [SupplyParameterFromForm] property to receive what the user typed.

for a middle

Explain the per-request rebuild: the POST is dispatched by form name, mapped by field name, and OnInitialized runs again, hence ??=.

for a senior

Diagnose 400s, ambiguous-name exceptions and lost values; use FormMappingScope, per-form FormName on the attribute and DTOs against overposting.

for a principal

Decide which forms stay static SSR for resilience and cost, and set naming conventions so forms survive a later switch between static and interactive rendering.

## Why static SSR forms need extra wiring A Blazor component with no `@rendermode` is rendered with **static server-side rendering**: the server runs the component once per request, writes HTML and forgets it. No circuit or WebAssembly runtime is listening when the user presses submit, so the browser sends an ordinary HTTP POST to the page's URL. The server builds the component again from scratch and must answer three questions: 1. **Which form was submitted?** A page may hold several. 2. **Where do the posted values go?** The new component instance has no memory of the old one. 3. **Is the post genuine?** That is the antiforgery check. `FormName` answers the first, `[SupplyParameterFromForm]` the second, and the antiforgery token that `EditForm` renders the third. ## FormName: naming the submit event Under static SSR `EditForm` renders `method="post"`, registers its `onsubmit` handler as a **named event** using `FormName`, and emits a hidden field carrying that name plus an `AntiforgeryToken`. When the POST arrives, the endpoint renderer looks the name up: | Situation | Result | |---|---| | No form name in the POST | 400; the explanatory text is written only in Development | | A name no form on the page currently has | 400 ("Cannot submit the form ... because no form on the page currently has that name") | | Two forms with the same name in one scope | `InvalidOperationException`, a server error, because the app is at fault | | Exactly one match | The form's submit handler runs, after validation | The check happens only when a POST arrives, never at render time, so a missing name surfaces on the first real submit. Under interactive rendering `FormName` is unused, but giving every form a unique one keeps it working if interactivity is later removed. Forms from a library that might collide can be wrapped in `<FormMappingScope Name="...">`, which qualifies their names. ## [SupplyParameterFromForm]: getting the values back A property marked `[SupplyParameterFromForm]` is filled from the posted form data by Blazor's own form mapper; it need not be public. Two settings refine it: - `Name` - the form-field prefix to read; defaults to the property name. - `FormName` - accept values only from the form with this name, useful with two forms on one page. The mapping lines up because components derived from `InputBase<TValue>` render a `name` attribute from their `@bind-Value` expression: `@bind-Value="Volunteer.Email"` posts as `Volunteer.Email`. A raw `<input>` needs that name written by hand. Values that do not convert, such as `abc` for an `int`, become validation messages ("The value 'abc' is not valid for ...") and the input keeps the attempted text. This mapper is not MVC model binding: `[BindNever]` and `[BindRequired]` are ignored. Guard against overposting with a form DTO that exposes only editable fields. ## Initialization runs on every request `OnInitialized` runs on the GET and again on each POST, because every request creates a new instance. The mapped value is assigned before `OnInitialized`, so: - `Volunteer ??= new();` keeps posted data and creates a model only for the GET. - `Volunteer = new();` silently wipes everything the user typed, which looks exactly like "the values are lost". - Loading defaults from a database belongs behind a check that the post did not already supply them. The Identity pages in the `blazor` template follow the same `Input ??= new()` shape. ## Diagnosing it in production Outside Development the 400 carries an empty body, so the symptoms differ from what a developer saw locally: - **Every submit of one form returns 400.** Check the rendered HTML for the hidden `_handler` field; if it is missing, the form has no `FormName`. - **Occasional 400s right after a deployment.** A visitor submitted a page rendered by the previous build whose form name no longer exists; keep form names stable across releases. - **A 500 naming two components.** Two forms share a name in one scope, often a reused component rendered twice; give each instance a distinct name or scope. - **The handler runs but the model is empty.** The property lacks `[SupplyParameterFromForm]`, or a raw `<input>` has no `name` matching the property path. - **The handler runs but values revert.** `OnInitialized` or `OnParametersSet` overwrites the mapped model. ## Antiforgery `EditForm` renders the token itself; the app must call `app.UseAntiforgery()`, which the template does. Without that middleware, a Razor component endpoint, which carries antiforgery metadata, fails the request with an `InvalidOperationException`.

  • Two EditForms on one static page both post a VolunteerForm; how do you stop one form's values filling the other's model?
    Give each form a unique `FormName` and restrict each property with `[SupplyParameterFromForm(FormName = "...")]` matching its form. Without the restriction a property maps from any post in the current mapping scope, so the second model would be filled from the first form's submit.
  • A form inside a reusable Razor class library uses FormName="contact" and the host page has its own "contact" form; what happens and how do you fix it?
    Both forms register the same name in one scope, so a POST to it throws an `InvalidOperationException` listing both components. Wrap the library component in `<FormMappingScope Name="Library">`; its form name is then scope-qualified and no longer collides with the page's own form.

An apartment block's shared mailbox: a letter with no flat number cannot be delivered and comes back, and two flats with the same number make the porter refuse to guess. FormName is the flat number on each POST.

  • FormName only matters for interactive forms, not static SSR ones.
  • A missing FormName is reported when the page first renders.
  • Initializing the model with Volunteer = new() in OnInitialized is safe on a post.
  • [BindNever] on a property stops Blazor form mapping from setting it.
  • [SupplyParameterFromForm] properties must be public like [Parameter] ones.