我有一个显示“Room”对象的HTML表的视图。每个房间都是表格中的一行(这是一个PartialView)。
该视图包含以下代码:
<table>
@foreach (var item in Model)
{
using (Html.BeginForm())
{
@Html.Partial("_roomPartial", item)
}
}
</table>
模型是IEnumerable<Room>
,_roomPartial是包含房间属性的HTML行。
出于某种原因,Partial的HTML在结束表单标记之后呈现(尽管在使用块内部):
<form action="/Room/Index" method="post"></form>
<tr>
...some boring markup...
</tr>
我试图让其工作的其他事情:
Html.RenderPartial("_roomPartial", item);
代替@Html.Partial
。using (Html.BeginForm())
块
我每次都得到相同的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>
_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>
答案 0 :(得分:5)
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>