Open
Description
Given the following component:
@page "/null-forms"
<h3>Null Forms</h3>
<p>@message</p>
<EditForm Model="Input" FormName="main-form" OnSubmit="OnMainSubmit">
@if (!HideOptional)
{
<div>
<label>Optional input: </label>
<InputText @bind-Value="Input.Optional" />
</div>
}
<button type="submit">Submit primary</button>
</EditForm>
<form @formname="secondary-form" @onsubmit="OnSecondarySubmit" method="post">
<AntiforgeryToken />
<button type="submit">Submit secondary</button>
</form>
@code {
private string? message = null;
[SupplyParameterFromQuery]
private bool HideOptional { get; set; }
// Specifying the FormName works around the bug during secondary-form submission,
// but not the bug when there's no optional input loaded.
//[SupplyParameterFromForm(FormName = "main-form")]
[SupplyParameterFromForm]
private InputModel Input { get; set; } = new();
private void OnMainSubmit()
{
message = $"OnMainSubmit: {Input.Optional}";
}
private void OnSecondarySubmit()
{
message = "OnSecondarySubmit";
}
private sealed class InputModel
{
public string Optional { get; set; } = "";
}
}
You will see an error like the following:
InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.
Microsoft.AspNetCore.Components.Forms.EditForm.OnParametersSet()
This happens if you either click "Submit secondary" or click "Submit primary" with ?hideoptional=true
. You can work around the "Submit secondary" issue by specifying [SupplyParameterFromForm(FormName = "main-form")]
for the InputModel
, but I'm not sure about a workaround for "Submit primary" with ?hideoptional=true
other than putting Intput ??= new()
in OnInitialized()
.
This came up when working on the Identity Razor components for the .NET 8 project templates. DeletePersonalData.razor and Email.razor