繁体   English   中英

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

[英]MVC DisplayFor template table with duplicated headers

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

成为视图模型

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>

一种解决方案可以是:

为您的整个模型创建显示模板

@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列表,并且您可以轻松地遍历列表并创建表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM