Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear InputFields after clicked submit using Blazor

I wonder if any one knows how to clear all the input fields after push the save button "Submitted"?? When i return to the page my values are still there.

This is my code that i am using.

@page "/testform"
@inject BlazorApp6.Data.Person person
@inject BlazorApp6.DBContext.ValidationTestContext Context
@inject BlazorApp6.Interface.INterface Manager

<EditForm Model="@person" OnValidSubmit="@CreatePerson">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <div class="form-group">
            <label for="Name">Name</label>
            <InputText @bind-Value=Person.Name class="form-control" id="Name" />
            <ValidationMessage For=@(() => Person.Name) />
        </div>
        <div class="form-group">
            <label for="Age">Age</label>
            <InputNumber @bind-Value=Person.Age class="form-control" id="Age" />
            <ValidationMessage For=@(() => Person.Age) />
        </div>
        <input type="submit" class="btn btn-primary" value="Save" />
    </EditForm>

@code {



    protected override async Task OnInitializedAsync()
    {
        person.OnChange += StateHasChanged;
    }

    private void CreatePerson()
    {

        Manager.Create(person);
        
       
    }

}
like image 200
Anders Dahl Avatar asked Sep 19 '25 17:09

Anders Dahl


1 Answers

Reset all values. Easiest way:

private void CreatePerson()
{
    Manager.Create(person);
    person = new Person();            
}

and you shouldn't need StateHasChanged:

protected override async Task OnInitializedAsync()
{
    // person.OnChange += StateHasChanged;
}

otherwise you have to -= and += the event(s) around new Person()

like image 190
Henk Holterman Avatar answered Sep 23 '25 10:09

Henk Holterman