形式标记在foreach循环剃刀消失MVC 3(form tag disappear in fore

2019-10-17 09:50发布

我有在剃刀foreach循环形式的标签的问题,当渲染输出HTML是错误的,因为在第一次迭代的形式标记在这里消失是剃刀代码

@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>

        }

这里所呈现的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>

Answer 1:

你可能想开始一个新的标签现有里面,就像这样:

<form id="mainPageForm">
@foreach(item in items)
{
    <form id="nestedForm@(item.FormId)">
}
</form>

这导致了这样的事情:

<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


文章来源: form tag disappear in foreach loop razor MVC 3