“对象”不包含“用户名”的定义 [英] 'object' does not contain a definition for 'UserName'

查看:148
本文介绍了“对象”不包含“用户名”的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a ExistingUsers controller in MVC :

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

        return View(users);
    }

和下面的视图为上述控制器:

And the following view for the above controller:

@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>

但每当我给控制器的请求时,出现异常说:

But whenever i send a request to the controller, an exception occur says :

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>

怎么回事错在这里?

Whats going wrong here ?

推荐答案

这可能是因为你需要明确指定为项目的类型'for'循环 - 尝试:

It may be that you need to explicitly specify the type for the items in the 'for' loop - try:

@foreach (MembershipUser item in Model)

这篇关于“对象”不包含“用户名”的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆