Linq在MVC中选择了一些字段

时间:2015-11-15 21:22:17

标签: asp.net-mvc linq

我正在尝试选择一些字段并将它们发送到MVC View,但它给了我这个错误

传递到字典中的模型项的类型为System.Collections.Generic.List'1[<>f__AnonymousType6'2[System.String,System.String]],但此字典需要类型为System.Collections.Generic.IEnumerable'1[IdentitySample.Models.ApplicationUser]的模型项。

这是我的代码:

public ActionResult Index()
{
    var con = new ApplicationDbContext();
    var allusers = (from u in con.Users
            where u.Message != null
            select new { UserName = u.UserName, Message = u.Message });
    return View(allusers.ToList());
}

并在视图中:

@model IEnumerable<IdentitySample.Models.ApplicationUser>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.Message)</th>
        <th>@Html.DisplayNameFor(model => model.Email)</th>
        <th>@Html.DisplayNameFor(model => model.EmailConfirmed)
        </th>
        <th>@Html.DisplayNameFor(model => model.PasswordHash)
        </th>
        <th>@Html.DisplayNameFor(model => model.SecurityStamp)
        </th>
        <th>@Html.DisplayNameFor(model => model.PhoneNumber)
        </th>
        <th>@Html.DisplayNameFor(model => model.PhoneNumberConfirmed)
        </th>
        <th>@Html.DisplayNameFor(model => model.TwoFactorEnabled)
        </th>
        <th>@Html.DisplayNameFor(model => model.LockoutEndDateUtc)
        </th>
        <th>@Html.DisplayNameFor(model => model.LockoutEnabled)
        </th>
        <th>@Html.DisplayNameFor(model => model.AccessFailedCount)
        </th>
        <th>@Html.DisplayNameFor(model => model.UserName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Message)
        </td>
        <td>@Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>@Html.DisplayFor(modelItem => item.EmailConfirmed)
        </td>
        <td>@Html.DisplayFor(modelItem => item.PasswordHash)
        </td>
        <td>@Html.DisplayFor(modelItem => item.SecurityStamp)
        </td>
        <td>@Html.DisplayFor(modelItem => item.PhoneNumber)
        </td>
        <td>@Html.DisplayFor(modelItem => item.PhoneNumberConfirmed)
        </td>
        <td>@Html.DisplayFor(modelItem => item.TwoFactorEnabled)
        </td>
        <td>@Html.DisplayFor(modelItem => item.LockoutEndDateUtc)
        </td>
        <td>@Html.DisplayFor(modelItem => item.LockoutEnabled)
        </td>
        <td>@Html.DisplayFor(modelItem => item.AccessFailedCount)
        </td>
        <td>@Html.DisplayFor(modelItem => item.UserName)
        </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 })
        </td>
    </tr>
}

</table>

1 个答案:

答案 0 :(得分:0)

您的视图需要IEnumerable<IdentitySample.Models.ApplicationUser>,但您只需通过投影ApplicationUser在select new { UserName = u.UserName, Message = u.Message });

中提供匿名对象

最好的方法是针对不同的提议使用不同的视图。在每个视图中,选择适当的@model并在控制器方法中提供该模型的详细信息。