-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InvalidOperationException on EditForm when Model set to local variable that is null #11331
Comments
Thanks for contacting us, @rickus123. |
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:
This excludes the edit form from the render to tree, until after the model is set to an instance in |
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 |
@aaronhudon-ts totally agree. I just hit this error and error message for me was like ....... |
@mkArtakMSFT, re this:
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. |
Just hit this exception as well, glad to have found this reported issue as quickly as I did. |
Separate out the error messages for when no parameters were provided for Model or EditContext and when both are provided, to make it clearer to the user what the exact issue is. Addresses dotnet#11331
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. |
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.
The text was updated successfully, but these errors were encountered: