gpt4 book ai didi

asp.net-mvc - @Html.DisplayNameFor 详细模型

转载 作者:行者123 更新时间:2023-12-03 10:32:23 27 4
gpt4 key购买 nike

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

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 helper 在我的表的标题中显示 Carrier.Name
当我使用 IEnumerable 作为这个 View 的模型时,我有 @Html.DisplayFor(model => model.Name)
如何使用我的新 CarrierIndexViewModel 显示 Carrier.Name 的标题获得相同的结果?

最佳答案

您可以在 View 中添加 @using 语句以指定 Carrier 类型,然后您需要循环遍历模型的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com