为什么我的PartialView会在表单标记之外呈现?

Jos*_*ell 4 c# asp.net-mvc asp.net-mvc-5

我有一个视图,显示"房间"对象的HTML表格.每个房间都是表格中的一行(这是一个PartialView).

该视图包含以下代码:

<table>

    @foreach (var item in Model) 
    {
        using (Html.BeginForm())
        {
            @Html.Partial("_roomPartial", item)
        }
    }

</table>
Run Code Online (Sandbox Code Playgroud)

Model是an IEnumerable<Room>,_roomPartial是包含Room属性的HTML行.

出于某种原因,Partial的HTML在结束表单标记之后呈现(尽管在使用块内):

<form action="/Room/Index" method="post"></form>
<tr>
    ...some boring markup...
</tr>
Run Code Online (Sandbox Code Playgroud)

我试图让其工作的其他事情:

  • 用来Html.RenderPartial("_roomPartial", item);代替@Html.Partial.
  • using (Html.BeginForm())在PartialView中移动了块

我每次都得到相同的HTML结果.

为什么Partial渲染在表单标记之外?我怎样才能让它正确渲染?


视图:

@model IEnumerable<PatientAssigner.Models.Room>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isOccupied)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.patientComplexityLevel)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) 
{
    using (Html.BeginForm())
    {
        @Html.Partial("_roomPartial", item)
    }
}
</table>
Run Code Online (Sandbox Code Playgroud)

_roomPartial:

@model PatientAssigner.Models.Room

<tr>
    <td>
        @Html.DisplayFor(room => room.ID)
        @Html.HiddenFor(room => room.ID)
    </td>
    <td>
        @Html.EditorFor(room => room.isOccupied)
        @Html.ValidationMessageFor(room => room.isOccupied)
    </td>
    <td>
        @Html.DropDownListFor(room => room.patientComplexityLevel,
                                    new SelectList(Enum.GetValues(typeof(PatientAssigner.Models.PatientComplexityLevel)),
                                                    Model.patientComplexityLevel),
                                                    "")
        @Html.ValidationMessageFor(room => room.patientComplexityLevel)
    </td>
    <td>
        <input type="submit" value="Save" class="btn btn-default" id="saveBtn" />
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

Dav*_*idG 5

form标签在tabletr标签之间无效.我建议您将视图更改为没有trtd标记,并在外部执行此操作:

<table>

    @foreach (var item in Model) 
    {
        <tr><td>
        @using (Html.BeginForm())
        {
            @Html.Partial("_roomPartial", item)
        }
        </td></tr>
    }

</table>
Run Code Online (Sandbox Code Playgroud)

如果你需要form为每一行都有一个但是希望你input的跨越单元格,你唯一的选择就是拥有这样的嵌套表:

<table>
  <tr>
    <td>
      <form>
        <table>
          <tr>
            <td>cell1</td>
            <td>cell2</td>
            <td>cell3</td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
  <tr>
    <td>
      <form>
        <table>
          <tr>
            <td>cell1</td>
            <td>cell2</td>
            <td>cell3</td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)