本文介绍了MVC错误:类型为“模型项目System.Collections.Generic.IEnumerable`1要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些修改一个系统,我碰到的,我希望有人能获得帮助的例外?我得到的错误是:

I'm doing some modifications to a system, and I've run into an exception that I was hoping someone could help with?? The error I'm getting is the following:

异常详细信息:
  System.InvalidOperationException:该
  模型项目传递到字典
  是类型
  System.Collections.Generic.List 1 [System.String]',
  但本词典需要一个模型
  类型项目
  System.Collections.Generic.IEnumerable
1 [MyApp.Data.aspnet_Users]'。

基本上我想要做的是显示不活动的用户列表。下面是我为的书面控制器:

Basically what I want to do is display a list of Inactive users. Here is the controller I've written for that:

public ActionResult InactiveUsers()
        {
            using (ModelContainer ctn = new ModelContainer())
            {
                aspnet_Users users = new aspnet_Users();

                DateTime duration = DateTime.Now.AddDays(-3);

                var inactive = from usrs in ctn.aspnet_Users
                    where usrs.LastActivityDate <= duration
                    orderby usrs.LastActivityDate ascending
                    select usrs.UserName;

                return View(inactive.ToList());
            }

        }

从这里再我加了一个局部视图。这里是code它如果有人有兴趣。只是一般的添加视图 - >列表。

From here then I added a partial view. Here is the code for it if anyone is interested. Just the usual Add View --> List.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>" %>

    <table>
        <tr>
            <th></th>
            <th>
                ApplicationId
            </th>
            <th>
                UserId
            </th>
            <th>
                UserName
            </th>
            <th>
                LoweredUserName
            </th>
            <th>
                MobileAlias
            </th>
            <th>
                IsAnonymous
            </th>
            <th>
                LastActivityDate
            </th>
        </tr>

    <% foreach (var item in Model) { %>

        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.UserId }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.UserId })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.UserId })%>
            </td>
            <td>
                <%: item.ApplicationId %>
            </td>
            <td>
                <%: item.UserId %>
            </td>
            <td>
                <%: item.UserName %>
            </td>
            <td>
                <%: item.LoweredUserName %>
            </td>
            <td>
                <%: item.MobileAlias %>
            </td>
            <td>
                <%: item.IsAnonymous %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.LastActivityDate) %>
            </td>
        </tr>

    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>

和完整性这里是索引页,我使部分:

And for completeness here is the Index page where I render the partial:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Administrator.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <% Html.RenderAction("InactiveUsers"); %>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>

如果任何人有任何意见,我会非常感激:)

If anyone has any advice I'd be very grateful :)

推荐答案

您好我认为它因为在你的局部视图类型是 System.Web.Mvc.ViewUserControl&LT;&IEnumerable的LT; MyApp.Data.aspnet_Users&GT; &GT;
但你选择的用户名

Hi i think its because in your partial view type is System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>but you select user names

select usrs.UserName;

return View(inactive.ToList());

试着改变它选择的USR;但不usrs.UserName

Try change it to select usrs; but not usrs.UserName

这篇关于MVC错误:类型为“模型项目System.Collections.Generic.IEnumerable`1要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:00