Skip to content

Model not created on post with EditForm #61341

Open
@pekspro

Description

@pekspro

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have a model like below that I bind to an EditForm (just the name property). But on post the model is not created.

    public class MyModel
    {
        public string? Name { get; set; }

        // Removing this line fixes the issue.
        public MyModel? Parent { get; set; }
    }

Expected Behavior

Model should be created, or it should fail or there should be some logging explain they issue.

Steps To Reproduce

  • Create a new Blazor project.
  • Open Home.razor, and replace with the code below.
  • Run the application.
  • Enter a name and press enter.
  • Check in OnInitialized if Model is created.
  • Remove the Parent property and see the difference.

Repro project: https://github.com/pekspro/AspNetIssues/tree/Issue61341

@page "/"
@inject ILogger<Home> Logger;

<PageTitle>Home</PageTitle>

<EditForm EditContext="FormContext" OnSubmit="Submit" FormName="RequestForm">
    <h4>Name</h4>
    <p>
        <InputText @bind-Value="Model!.Name" />
    </p>
</EditForm>

@code {
    [SupplyParameterFromForm(FormName = "RequestForm")]
    private MyModel? Model { get; set; }

    private EditContext FormContext = null!;

    public class MyModel
    {
        public string? Name { get; set; }

        // Removing this line fixes the issue.
        public MyModel? Parent { get; set; }
    }

    protected override void OnInitialized()
    {
        // Model will aways be null, even when the form has been submitted.
        if (Model == null)
        {
            Model = new();
        }

        FormContext = new EditContext(Model);
    }

    private void Submit()
    {
        Logger.LogInformation($"Name: {Model?.Name}");
    }
}

Exceptions (if any)

No response

.NET Version

9.0.201

Anything else?

I have seen similar issues when I have model that is used by EF-core. Sometimes it work, other times it doesn't. Haven't found a simple why to replicate this. But in these situation the model is created, but all properties has the default values.

I situations like this I would prefer an error or some explanation in the logs, I haven't found any so far.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: ReproIndicates that the team needs a repro project to continue the investigation on this issuearea-blazorIncludes: Blazor, Razor Componentsinvestigate

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions