0

I have an EditForm that I would like to reset after the save button is clicked. I input data into the textboxes (InputText) on the EditForm. Each textboxes is binded to an object field so that a new object can be saved to my SQL Database.

<EditForm Model=@newSystem OnValidSubmit="HandleSubmit" >
    <DataAnnotationsValidator/>

    <label for="systemName">System Name:</label>
    <InputText id="systemName"  @bind-Value="@newSystem.SystemName" ></InputText>
    <ValidationMessage For="@(() => newSystem.SystemName)" />

    <label for="systemDescription">System Description:</label>
    <InputText id="systemDescription"  @bind-Value="@newSystem.SystemDescription"></InputText>
    <ValidationMessage For="@(() => newSystem.SystemDescription)" />

    <label for="systemAccessDetails">System Access Details:</label>
    <InputText id="systemAccessDetails"  @bind-Value="@newSystem.SystemAccessDetails"></InputText>
    <ValidationMessage For="@(() => newSystem.SystemAccessDetails)" />

    <label for="additionalInformation">Additional Information:</label>
    <InputText id="additionalInformation" @bind-Value="@newSystem.AdditionalInformation"></InputText>
    <ValidationMessage For="@(() => newSystem.AdditionalInformation)" />

    <label for="systemManagerName">System Manager Name:</label>
    <InputText id="systemManagerName"  @bind-Value="@newSystem.SystemManagerName"></InputText>
    <ValidationMessage For="@(() => newSystem.SystemManagerName)" />

    <label for="systemManagerEmailAddress">System Manager Email Address:</label>
    <InputText id="systemManagerEmailAddress"  @bind-Value="@newSystem.SystemManagerEmailAddress"></InputText>
    <ValidationMessage For="@(() => newSystem.SystemManagerEmailAddress)" />

    <label for="systemManagerContactNumber">System Manager Contact Number:</label>
    <InputText id="systemManagerContactNumber"  @bind-Value="@newSystem.SystemManagerContactNumber"></InputText>
    <ValidationMessage For="@(() => newSystem.SystemManagerContactNumber)" />

    <input type="checkbox" @bind="checkboxValue" /> Is this system retired?

    <div class="form-group" style="display:@(checkboxValue ? "block" : "none")">
        <label for="retiredDate">When did this system retire?</label>
        <input type="date" @bind="retiredDate"  />
    </div>

    <button type="submit" class="btn-primary">Add</button>

</EditForm>




@code {

    private bool checkboxValue;
    private DateTime retiredDate = DateTime.Now;

    SystemDatum newSystem = new SystemDatum();


    public void HandleSubmit(EditContext editContext)
    {
        
        var newSystem = (SystemDatum)editContext.Model;

        newSystem.DateAdded = DateTime.Now;

        if (checkboxValue == true)
        {
            newSystem.DateRetired = retiredDate;
        }
        else
        {
            newSystem.DateRetired = null;
        }
     
        systemService.AddSystem(newSystem);
      
    }

  

}

How can I reset each textbox back to null? I'm using blazor-server-side c# and EF.

MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31
Imogen
  • 85
  • 13
  • 3
    `this.newSystem = new SystemDatum();` as last line in `HandleSubmit` – Taco Feb 03 '23 at 16:02
  • Take a look at this Question and Answer that describes a methodology for dealing with Edit Forms. https://stackoverflow.com/questions/75157902/managing-state-and-preventing-blazor-navigation-in-an-edit-form – MrC aka Shaun Curtis Feb 05 '23 at 10:34

0 Answers0