Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor - Static SSR Edit Form two-way databinding not behaving correctly with nested types #59446

Open
1 task done
htmlsplash opened this issue Dec 11, 2024 · 0 comments
Open
1 task done
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@htmlsplash
Copy link

htmlsplash commented Dec 11, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Environment: .net 9, Blazor,
Context: Static SSR, EditForm databinding

When an edit form is submitted that binds to wrapper Properties (on the bound model) that drill through to the desired property using nested hierarchy, a null reference exception is thrown. This happens because the referenced "Nested" property (within the model) is null, but shouldn't be when it is set to a new object in the constructor of the Model (see Example below). From use case perspective, if I allocate memory for Nested in SomeModel class, it should be there when Data binder uses an instance of SomeModel class.

Ex:

`public class SomeModel
{
public NestedModel Nested { get; set; }

public SomeModel()
{
	Nested = new(); // allocate default
}

// the problematic setter
public string StrFieldWrapper
{
set
{
if (Nested.InternalModel is null) // Nested is null, why?
Nested.InternalModel = new();
Nested.InternalModel.StrField = value;
}
}
}`

Datainbinding Expression that throws:

<InputText class="form-control" @bind-Value="Model.StrFieldWrapper" />

When the setter is invoked, Nested is not allocated (is null) and the property throws.

Here's the project that reproduces that problem:
https://github.com/htmlsplash/BlazorWebAppTest

Run project, select Form Test 2 from the nav menu.
Fill-out both fields: Text input and select an option from the drop down.
Click submit, observe null reference exception.
Page Component, see: FormDataBindTest2.razor

Expected Behavior

See problem description.

Steps To Reproduce

See problem description.

Exceptions (if any)

Null reference exception

.NET Version

.net 9

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Dec 11, 2024
@htmlsplash htmlsplash changed the title Blazor - 2 way form databinding not behaving correctly with nested types Blazor - Static SSR Edit Form two-way databinding not behaving correctly with nested types Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

1 participant