如何使用DisplayFor从后端在我的视图中显示数据

时间:2015-03-23 10:58:37

标签: asp.net html-helper

如何在我的观看{ TextCode = "Hello", Description = "Hello, How are you ",Text = "Great" }

上显示此数据

我的代码是

@Html.DisplayFor(model => model.Texts[].Description);

我希望以这种方式显示它,但是如何指定确切的特定textCode,我不能将索引放在Texts[]中,因为索引可能会在我添加更多文本时稍后更改

2 个答案:

答案 0 :(得分:1)

您使用了model.Texts[]。看起来您正在使用List<Texts>

简单的foreach循环。试试这个

@foreach(var item in Model.Texts)
{
  @Html.DisplayFor(m => item.Description)
}

这循环遍历每个Texts

答案 1 :(得分:0)

你不能以这种方式使用数组,你必须迭代:

@foreach(var item in Model.Texts)
{
   @Html.DisplayFor(m => item.Description)
}