具有重复标头的MVC DisplayFor模板表

时间:2013-10-16 10:35:30

标签: asp.net-mvc razor

使用EditorFor模板并尝试创建表格,如何只为集合中的每个项目获取一个标题行而不是一个?

成为viewmodel

public class CashbackOfferViewModel
{
    public List<SingleCashbackOfferViewModel> Offers { get; set; }
}

在视图中我有

@Html.DisplayFor(m => m.Offers)

显示模板是

@using LMS.MVC.Infrastructure
@model LMS.MVC.Areas.Finance.Models.SingleCashbackOfferViewModel
<table class="dataGrid">
    <thead>
        <tr class="headerRow">
            <th>@Html.LabelFor(m => m.CampaignName)</th>  
            <th>@Html.LabelFor(m => m.PersonId)</th>
            <th>@Html.LabelFor(m => m.FullName)</ths>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>@Html.DisplayFor(m => m.CampaignName)</td>
            <td>@Html.DisplayFor(m => m.PersonId)</td>
            <td>@Html.DisplayFor(m => m.FullName)</td>          
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

一种解决方案可以是:

为整个模型创建显示模板

@using LMS.MVC.Infrastructure
@model LMS.MVC.Areas.Finance.Models.CashbackOfferViewModel
<table class="dataGrid">
    <thead>
        <tr class="headerRow">
            <th>CampaignName</th>  
            <th>PersonId</th>
            <th>FullName</ths>
        </tr>
    </thead>
    <tbody>
        @for(int i = 0; i < Model.Offers.Count; ++i)
        {
        <tr>
            <td>@Html.DisplayFor(m => m.Offers[i].CampaignName)</td>
            <td>@Html.DisplayFor(m => m.Offers[i].PersonId)</td>
            <td>@Html.DisplayFor(m => m.Offers[i].FullName)</td>          
        </tr>
        }
    </tbody>
</table>

另外我认为在您的代码中,您的模型必须是SingleCashbackOfferViewModel的列表,您可以轻松地遍历列表并创建表