What does this Blazor error message mean?

Viewed 1169

If I told you that "You require sunglasses or an umbrella but not both." you would have no idea if it is raining or not. When running my Blazor web application I get

EditForm requires a Model parameter, or an EditContext parameter, but not both

My code has:

<EditForm Model="@order" OnSubmit="OnSubmit">

so as far as I know I am only using a Model parameter. I think use of the word 'Or' in error messages should be banned. My guess is that something has auto generated EditContext. Has anybody got any idea where to look?

3 Answers

To add some more detail to Eric's answer, the relevant sections of code from EditForm that generated the offending message are:

        private bool _hasSetEditContextExplicitly;

        [Parameter] public object? Model { get; set; }

        [Parameter]
        public EditContext? EditContext
        {
            get => _editContext;
            set
            {
                _editContext = value;
                _hasSetEditContextExplicitly = value != null;
            }
        }

and the overridden OnParametersSet that tries to get an Editcontext if one isn't explicitly set. The first else if threw your error, so Model must be null!

        protected override void OnParametersSet()
        {
            if (_hasSetEditContextExplicitly && Model != null)
            {
                throw new InvalidOperationException($"{nameof(EditForm)} requires a {nameof(Model)} " +
                    $"parameter, or an {nameof(EditContext)} parameter, but not both.");
            }
            else if (!_hasSetEditContextExplicitly && Model == null)
            {
                throw new InvalidOperationException($"{nameof(EditForm)} requires either a {nameof(Model)} " +
                    $"parameter, or an {nameof(EditContext)} parameter, please provide one of these.");
            }

            // If you're using OnSubmit, it becomes your responsibility to trigger validation manually
            // (e.g., so you can display a "pending" state in the UI). In that case you don't want the
            // system to trigger a second validation implicitly, so don't combine it with the simplified
            // OnValidSubmit/OnInvalidSubmit handlers.
            if (OnSubmit.HasDelegate && (OnValidSubmit.HasDelegate || OnInvalidSubmit.HasDelegate))
            {
                throw new InvalidOperationException($"When supplying an {nameof(OnSubmit)} parameter to " +
                    $"{nameof(EditForm)}, do not also supply {nameof(OnValidSubmit)} or {nameof(OnInvalidSubmit)}.");
            }

            // Update _editContext if we don't have one yet, or if they are supplying a
            // potentially new EditContext, or if they are supplying a different Model
            if (Model != null && Model != _editContext?.Model)
            {
                _editContext = new EditContext(Model!);
            }
        }

The full EditForm code is here

Did you create an instance for the variable @order? You will get this error message if you defined @order but leave it null.

If that's the case, it relates to the admittedly awkward error message because "EditForm requires a Model parameter, or an EditContext parameter", but you provided neither.

See: ASP.NET Core Blazor forms and validation : Troubleshoot

When assigning a Model to the form, confirm that the model type is instantiated

I got a similar error when I used an async operation to build my model

System.InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.

The model was not quite ready. I resolved it by testing for null and showing a spinner. Very short wait, but it was needed.

@if (myModel == null)
{
    <div class="spinner"></div>
}
else
{
    <EditForm Model="@myModel">
    ...
}
Related