【发布时间】:2013-11-23 00:32:11
【问题描述】:
我希望在视图中呈现部分视图,由于我使用强类型视图,因此我需要在主视图中包含两个视图模型。
这是我的视图模型,其中包含部分视图使用的模型和主视图使用的模型:
public class PersonIndexViewModel
{
public PersonLookupViewModel PersonLookupViewModel { get; set; }
public List<PersonViewModel> PersonViewModel { get; set; }
}
这是利用它的视图:
@model ViewModels.PersonIndexViewModel
@{
ViewBag.Title = "Registered People";
}
<h2>@ViewBag.Title</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@Html.Partial("_LookupPartial", Model.PersonLookupViewModel);
<table>
<tr>
<th>
@Html.DisplayNameFor(m => m.PersonViewModel.personName)
</th>
<th>
@Html.DisplayNameFor(m => m.PersonViewModel.dob)
</th>
<th>
@Html.DisplayNameFor(m => m.PersonViewModel.address)
</th>
<th>
@Html.DisplayNameFor(m => m.PersonViewModel.internalNo)
</th>
</tr>
@foreach (var item in Model.PersonViewModel) {
<tr>
<td>
@Html.DisplayFor(i => i.PersonViewModel.personName)
</td>
<td>
@Html.DisplayFor(i => i.PersonViewModel.dob)
</td>
<td>
@Html.DisplayFor(i => i.PersonViewModel.address)
</td>
<td>
@Html.DisplayFor(i => i.PersonViewModel.internalNo)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
所以我的问题是因为PersonViewModel 是一个列表,所以上面的表达式似乎无法访问它们的属性。例如下面的personName 无法识别。
@Html.DisplayNameFor(m => m.PersonViewModel.personName)
【问题讨论】:
标签: c# asp.net-mvc