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);
}
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With