Blazor - Static SSR Edit Form two-way databinding not behaving correctly with nested types #59446
Open
1 task done
Labels
area-blazor
Includes: Blazor, Razor Components
Is there an existing issue for this?
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; }
// 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
The text was updated successfully, but these errors were encountered: