skip to content

In a Blazor Web App, what does EditForm's Enhance parameter change for a statically rendered form, and how does an interactive form submit instead?

level: seniorimportance: should knowfreq 34%

answer

  1. Enhance renders data-enhance on the form
  2. fetch the POST, patch the DOM
  3. only Blazor endpoints accept enhanced posts
  4. interactive submit is an event, not a POST
  5. data-permanent protects script-changed content

basics

~20 s

Enhance adds data-enhance, so a static SSR form posts through fetch and Blazor patches the response into the page instead of reloading it. An interactive form sends no HTTP POST at all: submit is an event handled in .NET.

solid answer

~40 s

With `<EditForm Enhance>` the form renders `data-enhance`, and Blazor's script intercepts the submit, sends the POST with `fetch` and merges the returned HTML into the current DOM, so the page does not fully reload and scroll position usually survives. The server side is unchanged: `FormName`, `[SupplyParameterFromForm]` and server validation still apply. It must be set on the form itself, it only works for posts to Blazor endpoints, and `data-permanent` protects elements a script changed. Interactively, `Enhance` and `FormName` have no effect: the submit is a DOM event dispatched to the component over the circuit or in WebAssembly, the model is bound directly, `[SupplyParameterFromForm]` receives nothing, and fields validate as they change. In ASP.NET Core 10 a static form validates only on the server after the post.

code

razor · 15 lines
razor
@* Static SSR with enhanced posting *@
<EditForm Model="Volunteer" FormName="volunteer-signup"
          OnValidSubmit="RegisterAsync" Enhance>
    <DataAnnotationsValidator />
    <ValidationSummary />
    <InputText @bind-Value="Volunteer!.Email" />
    <button type="submit">Sign up</button>
</EditForm>

@* A hand-written form gets the same behaviour *@
<form method="post" @formname="newsletter" @onsubmit="Subscribe" data-enhance>
    <AntiforgeryToken />
    <input name="Email" />
    <button type="submit">Subscribe</button>
</form>

go deeper

for a junior

Recall that Enhance stops a static form reloading the page, and that interactive forms submit without an HTTP POST.

for a middle

Explain the fetch-and-patch mechanism, why FormName and form mapping still apply, and why they do nothing for an interactive form.

for a senior

Anticipate data-permanent needs, non-Blazor endpoint errors and the missing live validation of static forms in ASP.NET Core 10.

for a principal

Choose per form between enhanced static SSR and interactivity, weighing per-visitor circuits and downloads against live feedback.

## Three ways a Blazor form can submit A Blazor Web App can render the same `EditForm` in three ways, and the submit takes a different path in each: | Mode | What the browser does on submit | Who runs the handler | |---|---|---| | Static SSR, plain | Full-page HTTP POST and reload | A new component instance on the server, for this request only | | Static SSR with `Enhance` | `fetch` POST; Blazor patches the returned HTML into the page | Same as plain: a new server instance per request | | Interactive (Server, WebAssembly, Auto) | No HTTP POST; the submit is a DOM event | The live component instance, over the circuit or in WebAssembly | ## What Enhance does `Enhance` is a `bool` parameter on `EditForm`. When it is `true` the form renders a `data-enhance` attribute, which is exactly what you put on a hand-written `<form>` to get the same behaviour. Blazor's client script then: 1. Intercepts the submit event. 2. Sends the form body with `fetch` to the form's action. 3. Receives the server-rendered HTML and **merges it into the existing DOM** instead of replacing the document. The user sees the validation messages or the thank-you text appear without a full reload, and usually keeps the scroll position. Nothing changes on the server: the request is still a POST that needs a `FormName`, still maps values through `[SupplyParameterFromForm]`, still carries the antiforgery token `EditForm` renders, and still validates when the handler is dispatched. Rules worth stating in an interview: - **Not inherited.** Enhanced form handling is not hierarchical; `data-enhance` on a parent `div` does not enhance the forms inside it. - **Blazor endpoints only.** Posting an enhanced form to a non-Blazor endpoint, such as an MVC action, results in an error. - **DOM merging can undo scripts.** Content a script changed client-side may be reverted by the patch; mark it `data-permanent` to keep it. - **Opt-in for forms.** Links get enhanced navigation by default; forms are enhanced only with `Enhance` or `data-enhance`. - **Off switch.** Remove the parameter or set `Enhance="false"`. ## What changes when the form is interactive When the component runs with an interactive render mode, the form is live. Pressing submit raises a DOM event that Blazor dispatches to the component instance already in memory: over the SignalR circuit for Interactive Server, or inside the browser for Interactive WebAssembly. Consequences: - `Enhance` has no effect; there is no page reload to avoid. - `FormName` is not used, and no antiforgery hidden field is needed for the event. - `[SupplyParameterFromForm]` receives nothing, because no form POST reaches the server; the model is updated directly by `@bind-Value` as inputs change. - Field validation runs on each change, so messages can appear before submit. A component meant to work both ways therefore keeps the static-SSR shape, `[SupplyParameterFromForm]` with `Volunteer ??= new()`, and it also behaves interactively, because the property simply stays the model the inputs bind to. ## Validation feel in ASP.NET Core 10 In ASP.NET Core 10 a static SSR form, enhanced or not, has no live field validation between requests: rules run on the server when the post arrives, and the patched page shows the messages. If immediate feedback while typing matters for a form, that form needs an interactive render mode. ## One component, both modes A volunteer sign-up page that should work statically today and interactively later can be written once: 1. Give the form a unique `FormName` and mark the model `[SupplyParameterFromForm]`. 2. Initialize with `Volunteer ??= new()` so a post's mapped values survive. 3. Put the save logic in `OnValidSubmit`, which both paths call after validation. 4. Add `Enhance` for the static path; it is ignored once the page is interactive. The page then behaves correctly whichever render mode the host assigns, and switching modes becomes a one-line change in the page or the app's root. ## Choosing - Prefer **static SSR with `Enhance`** for simple, high-traffic forms such as a public sign-up: no circuit per visitor, no WebAssembly download, and no full reload. - Choose **interactive** forms when fields depend on each other live, when a file input needs `InputFile`'s change event, or when per-keystroke feedback matters.

  • After switching a static sign-up form to Enhance, a counter that a script inserted into the page header resets on every submit; why, and what is the fix?
    The enhanced post merges the server's HTML into the DOM, and the server's markup does not contain what the script added, so the merge reverts it. Mark the element `data-permanent` so Blazor leaves its content alone during enhanced updates.
  • A team adds Enhance to an EditForm whose action posts to an MVC controller; what happens?
    Enhanced form posts only work with Blazor endpoints, so the post results in an error. Either post to a Razor component page that handles the form, or drop `Enhance` so the browser performs a normal full-page POST to the controller.

  • Enhance makes a static form validate fields as the user types.
  • Putting data-enhance on a parent div enhances every form inside it.
  • Enhanced forms skip FormName because the post goes through fetch.
  • An interactive EditForm still sends an HTTP POST that fills [SupplyParameterFromForm].
  • Enhance switches the form to an interactive render mode.

Assumes ASP.NET Core 10; enhanced form handling arrived with Blazor Web Apps in .NET 8, and static SSR forms in 10 still validate only on the server after a post.