The ModelErrorMessage component displays model state error messages for a specific key, matching the ASP.NET Web Forms <asp:ModelErrorMessage> control. It renders a <span> containing the error text when errors exist for the given key, and nothing when there are no errors.
Original Microsoft documentation for the ASP.NET ModelErrorMessage control is at: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.modelerrormessage?view=netframework-4.8
ModelStateKey— The key in the model state / EditContext to display errors forAssociatedControlID— The ID of the associated input control, used withSetFocusOnErrorSetFocusOnError— Whentrue, focuses the associated control when an error is displayedCssClass— CSS class applied to the rendered<span>Style— Inline style applied to the rendered<span>ToolTip— Title attribute on the rendered<span>Enabled— Enable or disable the componentVisible— Show or hide the component
IsValid/Validate()— ModelErrorMessage is a display-only control; validation logic is handled by the EditContextHeaderText/HeaderStyle— Not part of the original ModelErrorMessage control
<asp:ModelErrorMessage
runat="server"
ModelStateKey="string"
AssociatedControlID="string"
CssClass="string"
SetFocusOnError="True|False"
ID="string"
Visible="True|False"
/><ModelErrorMessage
ModelStateKey="NewPassword"
AssociatedControlID="password"
CssClass="text-danger"
SetFocusOnError="true" />When errors exist for the specified ModelStateKey, ModelErrorMessage renders:
<span class="text-danger">The error message text</span>When there are no errors, nothing is rendered.
ModelErrorMessage requires a cascading EditContext — it must be placed inside an <EditForm>. It listens for validation state changes and automatically updates when errors are added or removed.
In Web Forms, model state errors are added in code-behind via ModelState.AddModelError(). In Blazor, the equivalent pattern uses ValidationMessageStore on the EditContext:
@using BlazorWebFormsComponents.Validations
<EditForm Model="@model" OnValidSubmit="HandleSubmit">
<input id="password" type="password" @bind="model.NewPassword" />
<ModelErrorMessage
ModelStateKey="NewPassword"
AssociatedControlID="password"
CssClass="text-danger"
SetFocusOnError="true" />
<Button Text="Submit" />
</EditForm>
@code {
private MyModel model = new();
private EditContext editContext;
private ValidationMessageStore messageStore;
protected override void OnInitialized()
{
editContext = new EditContext(model);
messageStore = new ValidationMessageStore(editContext);
}
private void HandleSubmit()
{
messageStore.Clear();
if (string.IsNullOrEmpty(model.NewPassword))
{
// Equivalent to ModelState.AddModelError("NewPassword", "...")
messageStore.Add(editContext.Field("NewPassword"),
"Password is required.");
editContext.NotifyValidationStateChanged();
}
}
}If multiple errors are added for the same key, they are displayed separated by <br> tags within a single <span>.
Migration from Web Forms to Blazor is straightforward:
- Remove the
asp:prefix andrunat="server"attribute - Wrap the form in an
<EditForm>(replacing<form runat="server">) - Replace
ModelState.AddModelError()calls withValidationMessageStore.Add()
<asp:ModelErrorMessage runat="server"
ModelStateKey="NewPassword"
AssociatedControlID="password"
CssClass="text-danger"
SetFocusOnError="true" /><ModelErrorMessage
ModelStateKey="NewPassword"
AssociatedControlID="password"
CssClass="text-danger"
SetFocusOnError="true" />!!! tip "Code-behind migration"
The biggest change is in the code-behind: Web Forms uses ModelState.AddModelError(key, message) while Blazor uses ValidationMessageStore.Add(editContext.Field(key), message) followed by editContext.NotifyValidationStateChanged().