user247702
user247702

Reputation: 24212

Element 'tr' cannot be nested within element 'tr'

I have a partial view like this:

@model List<user>

@foreach (var user in Model)
{
    <tr>
        <td>@user.name</td>
        <td>...</td>
    </tr>
}

And get an error like this:

Validation (HTML5): Element 'tr' cannot be nested within element 'tr'.

It's annoying me more than it should, but I want to get rid of it. Installing Web Standards Update didn't help. Any ideas?

Edit This is the main view:

<table>
    <thead>
        <tr>
            <th>@i18n.name</th>
            <th>...</th>
        </tr>
    </thead>
    <tbody id="results">
        @Html.Partial("list_rows", @Model.users)
    </tbody>
</table>

This is the generated HTML:

<table>
    <thead>
        <tr>
            <th>naam</th>
            <th>...</th>
        </tr>
    </thead>
    <tbody id="results">


<tr>
    <td>...</td>
    <td>...</td>
</tr>
<tr>
    <td>...</td>
    <td>...</td>
</tr>

    </tbody>
</table>

Edit Pulling the entire page through the W3C validator gives

This document was successfully checked as HTML5!

Upvotes: 4

Views: 12621

Answers (2)

Brendan Vogt
Brendan Vogt

Reputation: 26018

Just make sure that your code looks something like this:

<table>

@foreach (var user in Model)
{
     <tr>
          <td>@user.name</td>
          <td>...</td>
     </tr>
}

</table>

It seems like you already have an open tr tag in which you are trying to add more tr tags. If you already have tr tags in your table, just make sure they are all closed before the loop starts:

<tr>..</tr>

Upvotes: 0

QQping
QQping

Reputation: 1370

This error appears when you open a <tr> element before you loop through your model. So far the code you postet is correct and free of errors.

Upvotes: 1

Related Questions