John S
John S

Reputation: 8331

EditForm requires either a Model parameter, or an EditContext parameter

How does one resolve this Blazor error?

EditForm requires either a Model parameter, or an EditContext parameter

I have created a minimally reproducible example below. The issue seems to be because the model is not instantiated...Why does the following simple page throw this error?

@page "/"
<EditForm @Model="@person">
    <input @bind="@person.FirstName" />
    <input @bind="@person.LastName" />
</EditForm>
@code 
{
    public Person person = new Person();
    protected override Task OnInitializedAsync()
    {
        person = new Person
        {
            FirstName = "Fred",
            LastName = "Flintstone"
        };
        return base.OnInitializedAsync();
    }
}

Upvotes: 7

Views: 11350

Answers (3)

qqtf
qqtf

Reputation: 711

This is not an answer on the error presented, which has been answered, but targets a different cause which can provoke the same error message

EditForm requires either a Model parameter, or an EditContext parameter

If refer to this issue that was raised with asp.net core. I was building up my form, focusing on the html-side, and wanted to have an in-between look. And encountered the same issue, yet I had defined an EditContext-parameter.

But it wasn't initialised yet in

  protected override void OnInitialized()


{
      SomeModel = new SomeModel();
      EditContext = new EditContext(SomeModel);
  }

with

<EditForm FormName="FakeLogin" EditContext="@EditContext" OnSubmit="Inloggen"></EditForm>

(and the properties of the model matching the fields of the form)

Upvotes: 0

willingdev
willingdev

Reputation: 9596

I got this error after adding an EditForm to NavMenu.razor in a .net 8 blazor ssr and I had to use EditContext and remove Model property from EditForm tag in NavMenu.razor only.

<EditForm OnSubmit="Submit" FormName="SearchForm" EditContext="editContext">
    <div class="form-floating mb-3">
        <InputText @bind-Value="vm!.SearchText" class="form-control" placeholder />
        <label>Title</label>
    </div>
</EditForm>

cs file :

[SupplyParameterFromForm] public SearchVm? vm { get; set; }
private EditContext? editContext;

protected override void OnInitialized()
{
    vm ??= new();
    editContext = new(vm);
}

public void Submit()
{
    if (vm!.SearchText!.IsEmpty() == false)
        NavigationManager.NavigateTo($"Search/{vm!.SearchText}");
}

IsEmpty() extension method :

public static class StringExtensions
{
    public static bool IsEmpty(this string input)
    {
        return string.IsNullOrEmpty(input.Trim());
    }
}

Upvotes: 1

Bennyboy1973
Bennyboy1973

Reputation: 4208

I can not only tell you the error, but tell you how to check it while typing in VS.

Change to this:

<EditForm Model="@person">

(i.e. with no "@" sign on Model)

When you're typing in the <Editform> line, when you press space, you'll see the list of expected variables, several of which start with @, like @ref, and several which do not, like Model.

Upvotes: 4

Related Questions