Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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# ';对象';不包含';用户名';_C#_Asp.net Mvc 3 - Fatal编程技术网

C# ';对象';不包含';用户名';

C# ';对象';不包含';用户名';,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我已经在MVC中创建了一个ExistingUsers控制器: public ActionResult ExistingUsers() { MembershipUserCollection users = Membership.GetAllUsers(); return View(users); } 以及上述控制器的以下视图: @model MembershipUserCollection @{ ViewBag.Title =

我已经在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>
@model MembershipUserCollection
@{
ViewBag.Title=“现有用户”;
}
现有用户
用户名
创建日期
电子邮件
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.UserName)
@DisplayFor(modelItem=>item.CreationDate)
@DisplayFor(modelItem=>item.Email)
}
}
但每当我向控制器发送请求时,就会出现一个异常,即:

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>
Description:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码。
编译器错误消息:CS1061:“对象”不包含“用户名”的定义,并且找不到接受“对象”类型的第一个参数的扩展方法“用户名”(是否缺少using指令或程序集引用?)
源错误:
第25行:
第26行:
第27行:@Html.DisplayFor(modelItem=>item.UserName)
第28行:
第29行:

这里出了什么问题?

可能是您需要明确指定“for”循环中项目的类型-请尝试:

@foreach (MembershipUser item in Model)

可能需要显式指定“for”循环中项目的类型-try:

@foreach (MembershipUser item in Model)

听起来像
MembershipUserCollection
不公开类型化枚举数。听起来像
MembershipUserCollection
不公开类型化枚举数。值得注意的是,一旦这样做,IntelliSense应该建议属性名。如果不这样做,类型可能是问题所在。出于这个原因,我尽量避免使用
var
(隐式类型)。值得注意的是,一旦这样做,IntelliSense应该建议属性名称。如果不这样做,类型可能是问题所在。出于这个原因,我尽量避免使用
var
(隐式类型)。