Blazor 8 Edit Form can´t find Model or EditContext

853 Views Asked by At

In Blazor 8 I have a component with an Edit Form.

Whenever I submit the Form, I always get the following error:

InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.

This is how the component looks (uses SSR Server Mode):

<Sheet cssClass="mb-3">
    <SheetTitle Text="Admin Area"></SheetTitle>
    @if (Mini.Blocked)
    {
        <div>This Miniature has been blocked by an Admin</div>
    }
    else
    {
        <EditForm Model="@formData" FormName="BlockMiniature" method="post" OnSubmit="@OnBlockSubmit">
            <input type="hidden" value="@Mini.Id" name="formData.MiniatureId"/>
            <TextareaField Name="formData.Reason" @bind-Value="formData.Reason"></TextareaField>
            <div class="flex justify-center">
                <Button type="submit">Block Miniature</Button>
            </div>
        </EditForm>
    }
</Sheet>

@code {
    [Parameter]
    public Miniature Mini { get; set; }

    [SupplyParameterFromForm]
    public BlockMiniatureForm formData { get; set; } = new();
    
    public class BlockMiniatureForm
    {
        public Guid MiniatureId { get; set; }
        public string Reason { get; set; }
    }

    public async Task OnBlockSubmit(EditContext arg)
    {
        BlockMiniatureForm form = (BlockMiniatureForm) arg.Model;
        await mediatr.Send(new BlockMiniatureCommand(form.MiniatureId, form.Reason));
    }

}

I tried to add the Parameters to the Form with @ and without. Makes no difference :(

2

There are 2 best solutions below

2
On

I had the same problem in scafolded CRUD pages. I've fixed it by setting render mode as such:

@rendermode InteractiveServer
0
On

Not sure if this is your issue, but I've found mine. In my comment above, the EnableAuthenticator page caused the same error. I finally discovered the inclusion of another in a component on my layout - for a global search was the issue. Removing the second form fixed the issue. For me another account management specific layout without the second was an acceptable approach. But I doubt its a great answer in all cases.