Blazor app, how to add model state validation error?

Viewed 2579

How do I add a custom validation message in the SaveItem event (prefer not to use data annotations)? It should show up in the "ValidationMessage For=" context.

@using System.ComponentModel.DataAnnotations
@page "/edititem"


<EditForm Model="@model" OnSubmit="@Submit" OnValidSubmit="@SaveItem">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputText id="ItemName" @bind-Value="@model.ItemName" />
    <ValidationMessage For="@(() => model.ItemName)" />
    <button type="submit">Submit</button>
</EditForm>

@code {
    ItemModel model = new ItemModel();

    private void Submit()
    {

    }
    private void SaveItem()
    {


    }

    public class ItemModel
    {
        [Required]
        public string ItemName{ get; set; }
    }
}
1 Answers
Related