Search code examples
blazorblazor-server-sideasp.net-blazor

Tried to Extend EditForm, But ChildContent is not rendering


I have tried to extend EditForm like

    MyEditFormBase: EditForm
    {
    }

and in MyEditForm.razor

    @inherits MyEditFormBase
    @ChildContent

when i use this like

    <MyEditForm Model="TestModel">
<div>
    <InputText @bind-Value="TestModel.Name"></InputText>
</div>
    </MyEditForm>

this renders like

Microsoft.AspNetCore.Components.RenderFragment`1[Microsoft.AspNetCore.Components.Forms.EditContext]

Solution

  • The EditForm's ChildContent is not a regular RenderFragment, but a generic RenderFragment of type RenderFragment<EditContext> (also commonly referred to as a Template).

    In order to render that, you have to pass the argument like so:

    @ChildContent(EditContext)
    

    The advantage of such a template is that you can then access the EditContext object from the child content via @context.