【问题标题】:MVC Error: model item of type 'System.Collections.Generic.IEnumerable`1 requiredMVC 错误:需要 'System.Collections.Generic.IEnumerable`1 类型的模型项
【发布时间】:2010-11-30 12:05:39
【问题描述】:

我正在对系统进行一些修改,但遇到了一个异常,我希望有人能提供帮助?我得到的错误如下:

异常详情: System.InvalidOperationException: 模型项传入字典 是类型 'System.Collections.Generic.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Data.aspnet_Users]'。

基本上我想要做的是显示非活动用户列表。这是我为此编写的控制器:

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

        }

然后我从这里添加了一个局部视图。如果有人感兴趣,这是它的代码。只是通常的添加视图 --> 列表。

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

如果有人有任何建议,我将不胜感激:)

【问题讨论】:

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


    【解决方案1】:

    嗨,我认为这是因为在您的局部视图中,类型是 System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;MyApp.Data.aspnet_Users&gt;&gt; 但是你选择了用户名

    select usrs.UserName;
    
    return View(inactive.ToList());
    

    尝试将其更改为选择用户;但不是 usrs.UserName

    【讨论】:

    • 辛苦了。谢谢@Sanja。
    • @TaraWalsh 没问题,问一下
    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多