Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp mvc中将列表传递给aview:list vs IEnumerable_C#_Asp.net - Fatal编程技术网

C# 在asp mvc中将列表传递给aview:list vs IEnumerable

C# 在asp mvc中将列表传递给aview:list vs IEnumerable,c#,asp.net,C#,Asp.net,只是一个简单的问题 @using MvcApplication6.Models; @model IEnumerable<Employee> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New record", "Create") </p> <p>@Html.ActionLink("Check dep

只是一个简单的问题

@using MvcApplication6.Models;
@model IEnumerable<Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New record", "Create")
</p>

<p>@Html.ActionLink("Check departments", "Index", "Department")</p>

<table border="1">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.firstname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.lastname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.department)
        </th>
        <th>Action</th>
    </tr>

@foreach (Employee item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.firstname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.lastname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.department)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
            @Html.ActionLink("Details", "Details", new { id=item.id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.id }) |
            @Html.DisplayFor(modelItem => item.Players[0])
        </td>

    </tr>


}

</table>
我在其他视图上测试它,它被声明为一个列表:

@model List<MvcApplication6.Models.Employee>
@型号列表

尽管不涉及
DisplayName for
DisplayFor
,但它仍能正常工作。

IEnumerable
当您只需要迭代时,可以使用
IEnumerable
(完整列表请参见此处:),这意味着如果您只想对列表执行
foreach
,则应返回一个
IEnumerable

列表

您可以使用
List
获取需要迭代、修改、排序等的对象列表(完整列表请参见此处)

错误肯定会有助于了解问题的原因,而且我怀疑
@Html.DisplayNameFor(model=>model.id)
可能在计算所有类型时遇到问题,因为model不完全是
IEnumerable
@Html.DisplayNameFor(model => model.id)
@model List<MvcApplication6.Models.Employee>