asp.net-mvc - MVC错误: model item of type 'System.Collections.Generic.IEnumerable` 1 required

标签 asp.net-mvc ienumerable partial-views unhandled-exception

我正在对系统进行一些修改,并且遇到了一个异常,我希望有人可以帮助解决?我收到的错误如下:

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Data.aspnet_Users]'.

基本上我想要做的是显示非事件用户的列表。这是我为此编写的 Controller :

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());
            }

        }

从这里我添加了一个部分 View 。如果有人感兴趣的话,这是它的代码。只是通常的添加 View --> 列表。

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

为了完整起见,这里是我渲染部分内容的索引页面:

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

如果有人有任何建议,我将非常感激:)

最佳答案

嗨,我认为这是因为在您的部分 View 中类型是 System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>> 但您选择用户名

select usrs.UserName;

return View(inactive.ToList());

尝试更改它以选择用户;但不是 usrs.UserName

关于asp.net-mvc - MVC错误: model item of type 'System.Collections.Generic.IEnumerable` 1 required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313518/

相关文章:

asp.net - 允许更广泛的输入范围后验证危险字符MVC模型字段的正确方法

asp.net-mvc - 布局和不同的内容和风格,复杂的MVC4应用程序

asp.net-mvc - Java Spring MVC 中的 ASP.NET MVC 部分 View

c# - 在具有多个应用程序池的iis服务器根目录下运行C# MVC应用程序

asp.net - MVC ViewModel 返回 ArgumentNullException

c# - ASP.NET 条形图标签

c# - 用其总和替换集合中的重复值

c# - 使用 LINQ 转换 IEnumerable 的集合

c# - 当 foreach 遍历它时无法从集合中添加/删除项目

javascript - 在部分包含选项后获取选项结束标记的数组