Skip to content
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

Closed
rickus123 opened this issue Jun 18, 2019 · 12 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. investigate

Comments

@rickus123
Copy link

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 (<EditForm Model="@song"...)

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.

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jun 18, 2019
@mkArtakMSFT
Copy link
Member

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

@mkArtakMSFT
Copy link
Member

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

@mkArtakMSFT mkArtakMSFT added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jun 18, 2019
@rickus123
Copy link
Author

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.

@mkArtakMSFT mkArtakMSFT removed the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jun 19, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.0.0-preview9, 3.1.0 Jul 17, 2019
@mkArtakMSFT mkArtakMSFT modified the milestones: 3.1.0, 3.1.0-preview1 Aug 5, 2019
@dazinator
Copy link

dazinator commented Aug 21, 2019

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

@mkArtakMSFT
Copy link
Member

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

@mkArtakMSFT mkArtakMSFT added the ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. label Sep 9, 2019
@ghost
Copy link

ghost commented Sep 12, 2019

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

@arivera12
Copy link

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

@tomRedox
Copy link

tomRedox commented Nov 14, 2019

@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?

@ghost
Copy link

ghost commented Nov 25, 2019

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".

@konvay
Copy link

konvay commented Nov 26, 2019

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.

tomRedox added a commit to tomRedox/AspNetCore that referenced this issue Nov 26, 2019
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
@shawty
Copy link

shawty commented Nov 27, 2019

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.

@bdnts
Copy link

bdnts commented Dec 21, 2019

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.

@ghost ghost locked as resolved and limited conversation to collaborators Jan 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: By Design Resolved because the behavior in this issue is the intended design. investigate
Projects
None yet
Development

No branches or pull requests

9 participants