【发布时间】:2018-05-19 05:23:22
【问题描述】:
我有一个 cshtml 视图和一些模型来查看列中的值。在 Name4 和 Name5 列中,我想显示第二个模型的值,但我不知道该怎么做。例如,现在我显示模型 Project.Models.Names 中的值,但在 Name4 和 Name5 列中,我想显示模型 Project.Models.Surnames 中的值。
@model IEnumerable<Project.Models.Names>
@{
ViewBag.Title = "Smt";
}
<h2>Smt</h2>
<table class="table">
<tr>
<th>
Name
</th>
<th>
Name1
</th>
<th>
Name2
</th>
<th>
Name3
</th>
<th>
Name4
</th>
<th>
Name5
</th>
<th>
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name3)
</td>
<td>
...value from second model
</td>
<td>
...value from second model
</td>
【问题讨论】:
-
为什么不创建一个包含姓名和姓氏的模型 Persons?
-
name 和 surname 是为这个问题随机选择的名字
标签: c# razor model ienumerable