'object'不包含'UserName'的定义

时间:2012-12-10 12:05:04

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

我在MVC中创建了一个ExistingUsers控制器:

    public ActionResult ExistingUsers()
    {
        MembershipUserCollection users = Membership.GetAllUsers();

        return View(users);
    }

以上控制器的以下视图:

@model MembershipUserCollection

@{
    ViewBag.Title = "ExistingUsers";
}

<h2>ExistingUsers</h2>

<table>
    <tr>
        <th>
            Username
        </th>
        <th>
            CreationDate
        </th>
        <th>
            Email
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreationDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                <a href="/Account/DeleteUser?UserName=@item.UserName">Delete</a>
            </td>
        </tr>
        }
    }
</table>

但每当我向控制器发送请求时,都会发生异常:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'object' does not contain a definition for 'UserName' and no extension method 'UserName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 25:         <tr>
Line 26:             <td>
Line 27:                 @Html.DisplayFor(modelItem => item.UserName)
Line 28:             </td>
Line 29:             <td>

这里出了什么问题?

1 个答案:

答案 0 :(得分:4)

可能需要在'for'循环中明确指定项目的类型 - 尝试:

@foreach (MembershipUser item in Model)