I am having a problem with the <text> tag. The following code will not run due to the </tr> tag near the bottom. If i remove it, it works but it then prints an incorrect table. If i leave it i get the following error: Encountered end tag “tr” with no matching start tag. Are your start/end tags properly balanced?
How can i tell razor to ignore such things?
(I also tried to add a text tag around the /tr and also around all html code but that produces this: Encountered end tag “text” with no matching start tag. Are your start/end tags properly balanced?
@{
int i = 0;
foreach (var item in Model.Model)
{
if (i % 2 == 0)
{
<text><tr class="alternate-row"></text>
}
else
{
<text><tr></text>
}
<td>
<input type="checkbox" />
</td>
<td>
@item.Firstname
</td>
<td>
@item.Surname
</td>
<td>
<a href="">george@mainevent.co.za</a>
</td>
<td class="options-width">
<a href="" title="Edit" class="icon-1 info-tooltip"></a><a href="" title="Edit" class="icon-2 info-tooltip">
</a><a href="" title="Edit" class="icon-3 info-tooltip"></a><a href="" title="Edit"
class="icon-4 info-tooltip"></a><a href="" title="Edit" class="icon-5 info-tooltip">
</a>
</td>
</tr>
}
}
Update with another question
Why does Razor even test html-tags?
You can do this
or you can set a variable that “holds” your extended html for the
<tr>tag like thisThats the simplest solution, or you can create a custom html helper.
More information: Creating Custom HTML Helpers
Update
Darin said too, what he would create a custom html helper.
I suggest that too, if you need that more than one time.
conclusion
first choice is to create a html helper, second is to use my first
approach (inline if statement) and at last to use a variable.
It does not really depends on “how often” you need that, but if you really need
that only one time, choose the first approach.
Every of the three solutions are correct, its your decision depending on the time
you have.
hope this helps you