EditForm in Blazor does not work on submit

Rob Bertora 20 Reputation points
2024-04-20T15:03:25.1633333+00:00

Below is the source code, brand new blazor project vs2022 , Version 17.9.5

put a break point on
string breakpointhere = "z";
examine xx variable - for the model, you will see that the MyTitle string is always null.

Such basic stuff not working, please help

@page "/test"

<h3>Test</h3>

<EditForm FormName="XXX" Model="@MyTest" OnSubmit="HandleFormSubmission">

<label for="Mytitle">Project Title</label>

<InputText id="Mytitle" @bind-Value="@MyTest.MyTitle"  />

<button type="submit">Submit</button>
```</EditForm>

@code {

public class Test_Model

{

public string MyTitle { get; set; }

}

Test_Model MyTest = new();

public void HandleFormSubmission(EditContext xx)

{

string breakpointhere = "z";

}


Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,395 questions
0 comments No comments
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,366 Reputation points Microsoft Vendor
    2024-04-25T09:41:34.8333333+00:00

    Hi @Rob Bertora,

    Please try to use @rendermode InteractiveServer instead of @rendermode RenderMode.InteractiveWebAssembly.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2024-04-20T18:34:28.4166667+00:00

    Try setting the render mode.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0

    https://learn.microsoft.com/en-us/aspnet/core/blazor/forms/?view=aspnetcore-8.0

    @page "/test"
    @inject ILogger<TestComponent> Logger
    @rendermode InteractiveWebAssembly
    <h3>TestComponent</h3>
    <EditForm FormName="XXX" Model="@MyTest" OnSubmit="HandleFormSubmission">
        <label for="Mytitle">Project Title</label>
        <InputText id="Mytitle" @bind-Value="@MyTest.MyTitle" />
        <button type="submit">Submit</button>
    </EditForm>
    @code {
        public class Test_Model
        {
            public string MyTitle { get; set; } = string.Empty;
        }
        Test_Model MyTest = new();
        public void HandleFormSubmission(EditContext xx)
        {
            Logger.LogInformation(MyTest.MyTitle);
            //string breakpointhere = "z";
        }
    }