我是MVC的新手。在这个页面;我通过循环列出属于“单位名称”的项目。我想做什么:我想将属于同一“单位名称”的项目分组。每个项目都有一个“单位名称”。一个单元可以有多个项目。所以我想把它分组。
我的cshtml代码如下:
@if (Model.Projects.Any())
{
foreach (var item in Model.Projects.ToList())
{
<div class="ProjectPartialBody" data-id="@item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> @item.tbl_Unit.Name</span>
</div>
<div class="tools">
<a href="#" class="collapse"> </a>
</div>
<div class="actions">
<a href="@Url.Action("Details", "Project", new {ID=item.ID })" class="btn btn-circle default">Details</a>
</div>
</div>
<div class="portlet-body">
<span><i class="fa fa-file"></i> @item.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: @(item.ContractStartDate != null ? item.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / @(item.ContractEndDate != null ? item.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %@item.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: @(item.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %@item.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: @(item.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
</div>
</div>
}
}
页面视图如下(我的视图是土耳其语,但你会明白我在说什么):
你能帮帮我吗?如果您要插入任何其他代码块,请告诉我。答案 0 :(得分:0)
首先需要在for语句中分组:
foreach(var unitGroup in Model.Projects.GroupBy(g => g.tbl_Unit.Name))
{
//...your MVC markup here
}
现在你需要更改标记,因为你需要放入另一个为项目生成标记的foreach循环。
所以有点类似的东西。
<div class="ProjectPartialBody" data-id="@item.ID">
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
<span><i class="glyphicon glyphicon-asterisk"></i> @item.tbl_Unit.Name</span>
</div>
<div class="tools">
<a href="#" class="collapse"> </a>
</div>
<div class="actions">
<a href="@Url.Action("Details", "Project", new {ID=item.ID })" class="btn btn-circle default">Details</a>
</div>
</div>
@foreach(var project in unitGroup.ToList())
{
<div class="portlet-body">
<span><i class="fa fa-file"></i> @project.Name</span>
<span><i class="fa fa-calendar"> Contract Start and End Dates: @(project.ContractStartDate != null ? project.ContractStartDate.Value.ToString("dd.MM.yyyy") : "-") / @(project.ContractEndDate != null ? project.ContractEndDate.Value.ToString("dd.MM.yyyy") : "-")</i></span>
<div class="well">
<div class="form-group">
<label>Cash Completion Rate: %@project.CashCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: @(project.CashCompletionRate)%;"></div>
</div>
</div>
</div>
<div class="well">
<div class="form-group">
<label>Physical Completion Rate: %@project.PhysicalCompletionRate</label>
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: @(project.PhysicalCompletionRate)%;"></div>
</div>
</div>
</div>
</div>
}
</div>
</div>
这里应该有一些错误,因为我对你的模型不太熟悉,但这应该是背后的一般想法。