Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditForm within a table does not render

Blazor vRC1

There appears to be subtleties of the EditForm component, where it will not render its contents in certain markup situations. For example, when an EditForm is placed within the <table> tags, nothing happens.

<table>
<thead>...</thead>
<EditForm Model="MyModel">
    @foreach(var item in MyModel.Items)
    {
        <tr><td>....</td></tr>
    }
</EditForm>
</table>

However wrapping the <table> with the EditForm everything renders as expected.

<EditForm Model="MyModel">
<table>
<thead>...</thead>   
    @foreach(var item in MyModel.Items)
    {
        <tr><td>....</td></tr>
    }
</table>
</EditForm>

I'm fine with the latter, however if the rendering engine is unable to handle the first example, it would be nice if it'd throw some sort of error to warn the developer the situation is not supported.

like image 315
Aaron Hudon Avatar asked Jul 27 '26 13:07

Aaron Hudon


2 Answers

Avoiding making form as a child element of a table . As a workaround , you can try add a <div> and move EditForm inside it(although it's not correct to put div nested in form ) , or you can put form inside table cell(inside <td> tag) .

<table>
    <thead>...</thead>
    <div>
        <EditForm Model="MyModel">
            @foreach(var item in MyModel.Items)
            {

            }
        </EditForm>
    </div>
</table>

Or :

<table>
    <thead>...</thead>
    <tr>
        <td>
            <EditForm Model="MyModel">
                @foreach(var item in MyModel.Items)
                {

                }
            </EditForm>
        </td>
    </tr>
</table>

But of course , it's better to have your table inside form .

like image 106
Nan Yu Avatar answered Jul 29 '26 22:07

Nan Yu


when an EditForm is placed within the tags, nothing happens.

That is because

<table>
  <EditForm Model="MyModel">
  </EditForm>
</table>

translates to

<table>
  <form>
  </form>
</table>

Blazor outputs this alright but most browsers won't render it. It is invalid HTML.

Putting a <div> around the form , like in @NanYu's answer, is a hack but it seems to work. Putting a <form> inside a <td> is completely valid.

So: not a Blazor / Razor issue, just invalid html.

it would be nice if it'd throw some sort of error to warn the developer the situation is not supported.

Building all (for different Browsers) into the razor engine would be a lot of work and seems unnecessary.

like image 35
Henk Holterman Avatar answered Jul 29 '26 21:07

Henk Holterman