Aspnetcore: InvalidOperationException on EditForm when Model set to local variable that is null

Created on 18 Jun 2019  路  12Comments  路  Source: dotnet/aspnetcore

The exception text seen in the browser debug window is "EditForm requires a Model parameter, or an EditContext parameter, but not both".

This is a result of when EditForm.OnParametersSet() checks the values of EditContext and Model for null and will thrown an InvalidOperationException, even when (as in my case) the 'Model' on EditForm is set to a local variable that is still null when the Parameter is set.

In my case, the page (to edit an object) is passed a parameter that is used to fetch the object (during OnParametersSetAsync) and assign it to the local variable, but that variable is still null during the OnParametersSet call.

In any event, the wording of the exception (EditForm requires a Model parameter...) is somewhat misleading because the 'Model' parameter IS set (to a variable that is still null). I had to resort to viewing the source code of EditForm to determine what was really happening.

To Reproduce

  1. using Microsoft.AspNetCore.Components Version 3.0.0.0
  2. Create Razor page with a local variable uninitialized (Song song;)
  3. Add an EditForm component that has the local variable (song) assigned to the Model property (

The 'song' variable is to be updated via a fetch during the OnParametersSetAsync call.

Note that this exception does not appear to interfere with the functioning of the page (in my case) but there could be unexpected behavior in some other scenario.

I can prevent the exception altogether by initializing the local variable to a new object (instead of null) but this doesn't seem right.

By Design area-blazor investigate

Most helpful comment

I just came across this error myself. The way I fixed it was to do this:

       @if(Model != null)
        {
        <EditForm Model="@Model " OnValidSubmit="SubmitData">

        </EditForm>
        }

This excludes the edit form from the render to tree, until after the model is set to an instance in OnInitializedAsync

All 12 comments

Thanks for contacting us, @rickus123.
@javiercn can you please look into this? Thanks!

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

EditFormIssue.zip

The attached zip file contains the complete project/solution to illustrate this error. Run the app and with the (Chrome) dev tools open, navigate to the Counter page to see the InvalidOpEx encountered in EditForm.OnParametersSet().

Note that this exception did _not_ occur when I put the same code (EditForm...) on the main (Index) page.

I just came across this error myself. The way I fixed it was to do this:

       @if(Model != null)
        {
        <EditForm Model="@Model " OnValidSubmit="SubmitData">

        </EditForm>
        }

This excludes the edit form from the render to tree, until after the model is set to an instance in OnInitializedAsync

After investigating this this seems to be by-design. The error message is exactly what is expected to happen.

It would be simpler to diagnose if a NullReferenceException was thrown if the EditForm's model property is null.

@aaronhudon-ts totally agree. I just hit this error and error message for me was like .......

@mkArtakMSFT, re this:

After investigating this this seems to be by-design. The error message is exactly what is expected to happen.

Whether it's by design or not, I think @rickus123 's original point still stands; the message for a null Model parameter says "EditForm requires a Model parameter, or an EditContext parameter, but not both" is misleading. The presence of upvotes on the comments on this case would seem to confirm that.

I've just lost 1.5 hours chasing this error message, the message led me to believe that I was somehow assigning both the EditContext and the Model, so that's what I was hunting for. Once you know the message is also related to not setting Model, then you can also read it that way, but it's really not clear, or not clear to me at least.

It seems like any one of the following (pretty simple to implement) options would fix this:

  • Have a separate message informing the user the Model parameter was null, and possibly with a link to show how the approach mentioned by @dazinator can be used to alleviate that issue while the page is loading - many people will encounter this while the page is being put together.
  • Change the existing error message to say "EditForm requires a Model parameter, or an EditContext parameter, but not both. Check that either the EditContext or the Model parameter is not null and that you have not set values for both the Model and EditContext parameters"
  • Or, just throw a null argument exception

Can we please get this case reopened?

Please don't close this. The exception message is misleading.
If the Model parameter is null, throw an exception saying "Model parameter cannot be null".

Just hit this exception as well, glad to have found this reported issue as quickly as I did.
As noted above, checking that your object is not null is one way to work around this. Another possibility would be to use a default instantiation, assuming it doesn't impact other parts of your code.

Snap, just tripped me up too, had the message made more sense I would have had it fixed in 30 seconds instead of spending best part of an hour searching for an answer only to find this github thread.

Just hit this error and resolved it by assigning an object to the member.
public Item item { get; set; } = new Item();
Since the solution was to resolve the null reference, I vote this is a NullReferenceException error.

Was this page helpful?
0 / 5 - 0 ratings