c# - 如何在列表的强类型 View 上使用 LabelFor

标签 c# asp.net-mvc-3 razor

当我使用 asp.net mvc 3 脚手架制作列表时。我有一个包含表格的 View 。该表的标题在 View 中进行了硬编码。我想使用 LabelFor,所以我得到了我需要的 l10n。

我尝试做的(但失败了)是:

@model IEnumerable<User>
<table>
 <tr>
   <th>
      @(Html.LabelFor<User, string>(model => model.Name)) <!--This line errors-->
   </th>
 </tr>

@foreach (var item in Model) {
<tr>
 <td>
  @Html.DisplayFor(modelItem => item.Name)
 </td>
</table>

它的错误是“IEnumerable 不包含名称的定义”……等等……

我如何使它工作?

最佳答案

尝试一些类似的

@(Html.LabelFor<User, string>(model => model.FirstOrDefault().Name))

关于c# - 如何在列表的强类型 View 上使用 LabelFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6703353/

相关文章:

asp.net-mvc-3 - ASP MVC 3 防止页面刷新时多次插入

c# - Entity Framework 核心线程安全吗?

c# - 验证上下文始终为 NULL?

c# - EF核心: how to model a relationship attribute?

exception - ASP.NET MVC : “An internal error occurred.” when loading certificate bytes with X509Certificate2

C# MVC 5 Razor 分页列表 : Selecting and displaying a specific page

CSS 不适用于 ASP MVC 元素

asp.net-mvc-3 - 在 Razor View 中与通用方法一起使用

c# - Entity Framework 6 尝试在重命名时删除不存在的索引

c# - 如何将空值从 cmd.Parameters.AddWithValue 在 asp.net 中传递给数据库