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 - [SupplyParameterFromForm] parameter null on empty form data (eg. unchecked InputCheckbox) #60837

Open
1 task done
hakenr opened this issue Mar 10, 2025 · 0 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@hakenr
Copy link
Member

hakenr commented Mar 10, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

With Static SSR and [SupplyParameterFromForm], the value of the parameter from constructor is lost and the parameter is assigned null when on "empty" form data (eg. unchecked InputCheckbox).

<EditForm Model="_model" FormName="my-form">
	<DataAnnotationsValidator />
	<ValidationSummary />
	<InputCheckbox @bind-Value="_model.RememberMe" />
	<button type="submit">Submit</button>
</EditForm>

@_model?.RememberMe

@code {
	[SupplyParameterFromForm] private InputModel _model { get; set; } = new();

	private record InputModel
	{
		public bool RememberMe { get; set; }
	}
}

When you click Submit, you get InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.

Expected Behavior

The example above should end up with a _model parameter assigned to a InputModel instance with the RememberMe property to false.

Steps To Reproduce

See the sample above.
The POSTed form data is

_handler=my-form&__RequestVerificationToken=...

Exceptions (if any)

No response

.NET Version

9.0.2

Anything else?

If I replace the constructor initialization with OnInitialized initialization, the property value is incorrect (true with the repro below):

<EditForm Model="_model" FormName="my-form">
	<DataAnnotationsValidator />
	<ValidationSummary />
	<InputCheckbox @bind-Value="_model.RememberMe" />
	<button type="submit">Submit</button>
</EditForm>

@_model?.RememberMe

@code {
	[SupplyParameterFromForm] private InputModel _model { get; set; }

	protected override void OnInitialized() => _model ??= new InputModel();

	private record InputModel
	{
		public bool RememberMe { get; set; } = true;
	}
}
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