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
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.
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.
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:
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.
Most helpful comment
I just came across this error myself. The way I fixed it was to do this:
This excludes the edit form from the render to tree, until after the model is set to an instance in
OnInitializedAsync