@model IEnumerable <>中的强类型助手

nom*_*mad 4 c# asp.net-mvc razor asp.net-mvc-4

为什么我无法在下面的代码中使用强类型助手?

@using ISApplication.Models
@model IEnumerable<PersonInformation>

@foreach (PersonInformation item in Model)
{
    @Html.LabelFor(model => model.Name) // Error here.
    @item.Name // But this line is ok

    @* and so on... *@
}
Run Code Online (Sandbox Code Playgroud)

错误消息是

The type of arguments for method '...LabelFor<>... ' cannot be inferred from the usage. Try specifying the type arguments explicitly.

有任何想法吗?谢谢.

PSL*_*PSL 8

试试这种方式.您需要从项目中访问名称.

@foreach (PersonInformation item in Model)
{
    @Html.LabelFor(x => item.Name); 
    @Html.DisplayFor(x =>item.Name)

}
Run Code Online (Sandbox Code Playgroud)

  • 请注意以后的参考 - 这将起作用,但不会为输入控件生成MVC所需的命名约定,因此在回发任何值时,它们将不会被解析为正确的属性(可能会以null形式出现) (2认同)