Skip to content

Blazor - InputBase holds onto stale model when complex models are used. #62426

@bonsall

Description

@bonsall

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When binding a complex model to one of blazors built in input components (inherits InputBase<T>) you can run into an issue where the input component is no longer holding a reference to the correct model, and therefore validation does not behave as expected. Sometimes the validation message does not display. 100% of the time the FieldCssClass will not be set correctly.

Expected Behavior

Validation messages and css classes should still be applied when the underlying model for a input component changes.

Steps To Reproduce

Consider a model like this

public class ComplexModel
{
    public string Property1 { get; set; }

    public ChildModel Child { get; set; } = new();
}

public class ChildModel
{
    [Required]
    public string? Name { get; set; }

    public int Id { get; set; }
}

and a blazor component like this

<EditForm OnSubmit="SubmitModel" Model="ComplexModel">
    <p>Select a child</p>
    <ul>
        @foreach(var child in ChildOptions)
        {
            <li><button type="button" @onclick="() => SelectChild(child)">Child @child.Id</button></li>
        }
    </ul>
    <label for="ChildName">Child Name</label>
    <InputText @bind-Value="Model.Child.Name"
                       id="ChildName" />
    <ValidationMessage For="Model.Child.Name" />
    <button type="submit">Submit</button>
</EditForm>
@code {
    private ComplexModel Model { get; set; } = new();

    private ChildModel[] ChildOptions { get; } = [
        new ChildModel() { Id = 1; }
        new ChildModel() { Id = 2; }
        new ChildModel() { Id = 3; }
    ];

    void SelectChild(ChildModel child)
    {
        Model.Child = child;
    }

    void SubmitModel()
    {
        // Do things
    }
}

After selecting one of the children the validation message for the required Name property may or may not display. 100% of the times I tested this the invalid class was not applied to the input.

Exceptions (if any)

No response

.NET Version

8 (assuming 9 is affected a well).

Anything else?

No response

Activity

added
Needs: ReproIndicates that the team needs a repro project to continue the investigation on this issue
on Jun 24, 2025
added
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
on Jun 24, 2025
bonsall

bonsall commented on Jun 24, 2025

@bonsall
Author

I have provided example code in the "Steps To Reproduce" section. Is this not good enough?

added
Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.
and removed
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
on Jun 24, 2025
javiercn

javiercn commented on Jun 24, 2025

@javiercn
Member

@bonsall thanks for contacting us.

We require a project as a public github repository that reproduce the issue as well as detailed steps on how to reliably reproduce the issue.

Otherwise, there is a chance that there is an important detail that is missing from the repro (even if not evident) or that we make a mistake in reproducing the issue, which complicates the diagnosis of any potential problem.

added
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
and removed
Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.
on Jun 24, 2025
dotnet-policy-service

dotnet-policy-service commented on Jun 24, 2025

@dotnet-policy-service
Contributor

Hi @@bonsall. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

dotnet-policy-service

dotnet-policy-service commented on Jun 30, 2025

@dotnet-policy-service
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Needs: ReproIndicates that the team needs a repro project to continue the investigation on this issueStatus: No Recent Activityarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bonsall@javiercn

        Issue actions

          Blazor - InputBase holds onto stale model when complex models are used. · Issue #62426 · dotnet/aspnetcore