2017-03-27 46 views
0

这可能是一个基本的问题,但我目前正在思考这一问题。如何在循环内访问DisplayName注释,而不是从Razor视图中的模型访问?

我通常使用:

@Html.LabelFor(model=>model.myProperty) 
@Html.DisplayFor(model=>model.myProperty) 

其中显示DisplayName注释,从视图模型的财产。另外DisplayFor显示值。

但是,如果我想遍历模型列表,然后做同样的事情呢?

@foreach (var item in Model.myPropertyList) 
{ 
    @Html.LabelFor(item=>item.myProperty) //No longer works 
    @Html.DisplayFor(item=>item.myProperty) // No Longer works. 
} 

应的代码是在foreach循环皮卡物业DisplayName和价值是什么。

我认识到,以下将显示值工作:

@item.myProperty 

编辑:

答案似乎是:

@Html.DisplayNameFor(modelItem=>item.myProperty) 
@Html.DisplayFor(modelItem=>item.myProperty) 

发现佣工在这里和我... 。团队努力....谢谢

+0

嗨。视图的模型是什么? –

回答

1

检查下面的一个

<div> 
     <table border="1" class="col-md-12 text-center table-font table-hover table-striped table" id="tblTemp"> 
      <thead> 
       <tr> 
        <th class="text-center" style="color:#000;"> 
         My Property For Label 
        </th> 
        <th class="text-center" style="color:#000;"> 
         My Property For Textbox 
        </th>       
       </tr> 
      </thead> 
      <tbody> 

    @foreach (var item in Model.myPropertyList) 
    { 
     <tr> 
      <td class="text-left"> 
       @Html.LabelFor(modelItem=>item.myProperty) //Should work 
      </td> 
      <td class="text-left"> 
       @Html.DisplayFor(modelItem=>item.myProperty) // Should work 
      </td> 
     </tr> 
     } 
    </tbody> 
    </table> 
</div> 
+0

感谢您的支持。这里仍然有问题。我怎样才能将DisplayName的值分配给一个变量,只是试图调试这个。 – SamJolly

+0

类型是LabelFor 不是ForEach中的“Item”的类型。求助到目前为止 – SamJolly

+0

DisplayFor工作,但不LabelFor ...这样。有任何想法吗?谢谢。基本上我想显示“DisplayName”的注释值,正如通常在表格中做的 – SamJolly

1

您可以使用此:

@Html.DisplayFor(model=>item.myProperty) 
+0

DisplayFor作品,但不是LabelFor ...像这样。有任何想法吗?谢谢。 – SamJolly

1
@Html.DisplayNameFor(modelItem=>item.myProperty) 
@Html.DisplayFor(modelItem=>item.myProperty) 
相关问题