title: Create Blazor custom component that requires EditForm
body: <p>I'm creating a survey generator that has the main Razor page called <code>Survey</code> and then from here I add the element of the survey. The component is working if I create the survey from code. Each component is using HTML tag like</p>
  <pre><code>@inherits ElementBase&lt;string&gt;
  &lt;input type=&quot;@ElementData.TextboxType.GetDescription()&quot; 
         name=&quot;@Name&quot; placeholder=&quot;@ElementData.PlaceHolder&quot; 
         @bind-value=&quot;@Value&quot; class=&quot;form-control @CssInternal @CssClass&quot;&gt;
  </code></pre>
  <p>All the custom components are derived from a ElementBase class that implements</p>
  <pre><code>[CascadingParameter] internal EditContext ElementEditContext { get; set; }
  [CascadingParameter] internal Survey _survey { get; set; }
  
  private FieldIdentifier _fieldIdentifier;
  
  [Parameter]
  public T Value
  {
      get
      {
          return _value;
      }
  
      set
      {
          _value = value;
          _survey.SetElementValue(Name, _value);
      }
  }
  
  [Parameter] public EventCallback&lt;T&gt; ValueChanged { get; set; }
  
  [Parameter] public Expression&lt;Func&lt;T&gt;&gt; ValueExpression { get; set; }
  
  protected override Task OnInitializedAsync()
  {
      if (ElementEditContext != null)
      {
          // omitted for simplicity
          ElementEditContext.OnFieldChanged += FieldChanged;
          ElementEditContext.OnValidationRequested += ValidationRequested;
      }
  
      _survey.ElementValuesChanged += SurveyGenerator_ElementValuesChanged;
  
      return Task.CompletedTask;
  }
  </code></pre>
  <p>In the <code>Survey</code> page</p>
  <pre><code>&lt;CascadingValue Value=&quot;this&quot;&gt;
      &lt;EditForm EditContext=&quot;@editContext&quot; OnValidSubmit=&quot;@ValidFormSubmitted&quot;&gt;
          &lt;table class=&quot;table&quot;&gt;
              &lt;tbody&gt;
                      @for (int i = 0; i &lt; Form.Elements.Count(); i++)
                      {
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;SurveyFieldGenerator 
                                       Element=&quot;@Form.Elements[i]&quot; /&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      }
                  @if (ChildContent != null)
                  {
                      @ChildContent
                  }
                  &lt;tr&gt;
                      &lt;td colspan=&quot;2&quot;&gt;
                          &lt;button class=&quot;btn btn-primary&quot; 
                                  @onclick=&quot;Submit&quot;&gt;Submit&lt;/button&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/EditForm&gt;
  &lt;/CascadingValue&gt;
  </code></pre>
  <p>Now, I'm trying to use some components but from a Razor page and I added this code</p>
  <pre><code>&lt;Survey ShowDebug=&quot;true&quot;&gt;
      &lt;Survey.Components.Checkbox ElementData=&quot;@config&quot; /&gt;
  &lt;/Survey&gt;
  </code></pre>
  <p>Here I get this error:</p>
  <pre><code>Unhandled exception rendering component: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.
  System.InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these.
     at Microsoft.AspNetCore.Components.Forms.EditForm.OnParametersSet()
     at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
     at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
  </code></pre>
  <p>I read the document <a href="https://chrissainty.com/creating-bespoke-input-components-for-blazor-from-scratch/" rel="nofollow noreferrer">Creating Bespoke Input Components for Blazor from Scratch</a> and the <a href="https://stackoverflow.com/questions/66410535/valuechanged-and-valueepression-in-blazor">post on Stackoverflow</a>.</p>
  <p>First, in the <code>OnInitialized</code> raises immediately an error because <code>ValueExpression</code> is <code>null</code>.</p>
  <pre><code>protected override void OnInitialized()
  {
      _fieldIdentifier = FieldIdentifier.Create(ValueExpression);
  }
  </code></pre>
  <p>So, how can I change my components or only the base class in order to fix this issue?</p>