我要为租户显示几项费用/费用,现在我想总结所有价值。这是我的代码。
@foreach (var item in Model.Contracts)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Apartment.MonthParkfee)
</td>
<td>
@Html.DisplayFor(modelItem => item.Apartment.MonthUtilityFee)
</td>
<td>
@Html.DisplayFor(modelItem => item.MonthlyRent)
</td>
<td>
@Html.DisplayFor(sumOfAllFees => MonthParkFee + MonthUtilityFee + MonthlyRent)
</td>
</tr>
}
MonthParkFee和MonthUtilityFee都在Apartment表中,而MonthlyRent在合约表中,但合约表包含Apartment表的外键。我怎样才能在剃刀中将这些值加在一起并渲染它们?
答案 0 :(得分:1)
定义一个变量来保存总和,如下所示:
@foreach (var item in Model.Contracts)
{
var sum = item.Apartment.MonthParkfee + item.Apartment.MonthUtilityFee +item.MonthlyRent;
<tr>
<td>
@Html.DisplayFor(modelItem => item.Apartment.MonthParkfee)
</td>
<td>
@Html.DisplayFor(modelItem => item.Apartment.MonthUtilityFee)
</td>
<td>
@Html.DisplayFor(modelItem => item.MonthlyRent)
</td>
<td>
@Html.Display("sumOfAllFees" => sum)
</td>
</tr>
}
或添加Sum属性以查看您要发送的模型,并从控制器本身发送Sum以在视图中显示。