Reputation: 1733
I have a problem with form tags in a foreach loop in razor when it render the output html is wrong because in the first iteration the form tags disappear here is the razor code
@foreach (var item in Model)
{
<tr>
<td>@if (item.IsMandatory)
{
<span class="label label-important">Obligatorio</span>
}
else
{
<span class="label">Opcional</span>
} </td>
<td>@Html.DisplayFor(modelItem => item.DocumentName)</td>
<td>
</td>
<td>
<form>
form here!
</form>
</td>
</tr>
}
here is the rendered html:
<table class="table table-striped">
<thead>
<tr>
<th>Requerido</th>
<th>Documento</th>
<th>Accion</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span class="label label-important">Obligatorio</span>
</td>
<td>Copia de cédula</td>
<td>
here form! <----------Problem Here
</td>
</tr>
<tr>
<td>
<span class="label label-important">Obligatorio</span>
</td>
<td>
Copia de otro documento de identidad (licencia, pasaporte, seguro)
</td>
<td>
<form novalidate="novalidate">
here form!
</form>
</td>
</tr>
Upvotes: 1
Views: 1497
Reputation: 3659
You're probably trying to start a new tag inside an existing one, like this:
<form id="mainPageForm">
@foreach(item in items)
{
<form id="nestedForm@(item.FormId)">
}
</form>
Which results in something like this:
<form id="mainPageForm">
<form id="nestedForm1"></form> --> this ends the first form
<form id="nestedForm2"></form> --> correctly formed
<form id="nestedForm3"></form> --> correctly formed
</form> --> closing tag without parent
Upvotes: 4