Why is my PartialView rendered outside the form tag?

I have a view that displays an HTML table of Room objects. Each room is a row (which is a PartialView) in the table.

The view contains this code:

<table>

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

</table>

The model is this IEnumerable<Room>, and _roomPartial is an HTML string containing the properties of the room.

For some reason, the HTML Partial displays a closing form after the tag (even though it is inside the usage block):

<form action="/Room/Index" method="post"></form>
<tr>
    ...some boring markup...
</tr>

Other things I tried to do:

  • Used Html.RenderPartial("_roomPartial", item);instead @Html.Partial.
  • Moved block using (Html.BeginForm())inside PartialView

I got the same HTML results every time.

Why does Partial go beyond the form tag? And how can I make it display correctly?


View:

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

_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>
+4
1

form table tr. , tr td :

<table>

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

</table>

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>
+5

All Articles