在我的模型中,我有一个实体

public class Carrier
{
public Guid CarrierId { get; set; }
public string Name { get; set; }
}

我也有一个ViewModel
public class CarrierIndexViewModel
{
public IEnumerable<Carrier> Carriers { get; set; }
public PagingInfo PagingInfo { get; set; }
}

我有一个强类型的(CarrierIndexViewModel)索引 View ,它假定使用PagingInfo显示一个Carrier表。

我正在尝试使用HTML帮助器在表标题中显示Carrier.Name
当我使用IEnumerable作为该 View 的模型时,@Html.DisplayFor(model => model.Name)
如何使用新的CarrierIndexViewModel显示Carrier.Name的标题来获得相同的结果?

最佳答案

您可以在 View 中添加@using语句以指定Carrier类型,然后将需要遍历Model的Carriers属性。

您的 View 将看起来像..

@model CarrierIndexViewModel
@using Carrier

@foreach(Carrier c in Model.Carriers)
{
 @Html.DisplayFor(model => c.Name)
}

关于asp.net-mvc - @ Html.DisplayNameFor有关详细信息模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9386987/

10-14 18:33
查看更多