编辑器模板的标题行

时间:2016-04-01 11:51:23

标签: asp.net-mvc-4 editorfor

我在MVC4中有一个看起来像这样的视图:

 @model List<Home.Models.Model>
@{
ViewBag.Title = "DPR";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("AgendaNotes", "Home", FormMethod.Post, new    { @id = "formDPR"}))
{
<table style="font-size:xx-small">
    <thead>
        <tr>
            <th>Name</th>
            <th>DOD ID</th>
            <th>Last Review</th>
            <th>Include?</th>
            <th>Reason</th>
            <th>Notes</th>
        </tr>
    </thead>
</table>
<div style="background-color:white">
    @Html.EditorForModel()
</div>
<div>

</div>
}

model.cshtml只是一行中的一些字段。我不想把标题放在那一行上,因为它们重复的次数与它们在列表中的次数相同。是否有一种简单的方法可以在编辑器模板中为模型的行创建标题?

我就这样做了:

 <table style="font-size:xx-small" class="table_body">
        <thead>
            <tr>
                <th>Name</th>
                <th>DOD ID</th>
                <th>Last Review</th>
                <th>Include?</th>
                <th>Reason</th>
                <th>Notes</th>
            </tr>
        </thead>
        @for (var i = 0; i < Model.Count; i++)
        {
            <tr>
                @Html.EditorFor(m=>Model[i])
            </tr>
        }
    </table>

在model.cshtml中,它简化了TD元素中的一些字段。

1 个答案:

答案 0 :(得分:1)

不是100%确定我理解正确。在视图中创建表头,然后在每个

中调用一个EditorTemplate
@using (Html.BeginForm("AgendaNotes", "Home", FormMethod.Post, new { @id = "formDPR" }))
{
  <table style="font-size:xx-small">
    <thead>
        <tr>
            <th>Name</th>
            <th>DOD ID</th>
            <th>Last Review</th>
            <th>Include?</th>
            <th>Reason</th>
            <th>Notes</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Details)
        {
            @testTemplate(item)
        }
    </tbody>
  </table>
}

然后将编辑器模板更改为仅一行,例如

@helper testTemplate(Details detail)
{
  <tr>
    <td>@detail.Propery1</td>
    <td>@detail.Propery2</td>
  </tr>
}

我使用内联助手来说明我的意思。现在你应该有一个包含一个标题和许多行的表