[英]MVC DisplayFor template table with duplicated headers
Using a EditorFor template and trying to create a table, how can I get only one header row instead of one for each item of the collection? 使用EditorFor模板并尝试创建表,我如何只获得一个标题行而不是该集合的每个项目的标题行?
Being the viewmodel 成为视图模型
public class CashbackOfferViewModel
{
public List<SingleCashbackOfferViewModel> Offers { get; set; }
}
In the view I have 我认为
@Html.DisplayFor(m => m.Offers)
And the display template is 显示模板是
@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>
One solution can be: 一种解决方案可以是:
Create display template for your whole model 为您的整个模型创建显示模板
@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>
Also i think in your code, your model must be List of SingleCashbackOfferViewModel
and you can easily iterate over the list and create table 我也认为在您的代码中,您的模型必须是SingleCashbackOfferViewModel
列表,并且您可以轻松地遍历列表并创建表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.