MVC 错误:需要类型为“System.Collections.Generic.IEnumerable”1 的模型项

发布于 2024-10-04 18:23:54 字数 3601 浏览 0 评论 0 原文

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

异常详细信息: System.InvalidOperationException: 传递到字典中的模型项 属于类型 'System.Collections.Generic.List1[System.String]', 但这本字典需要一个模型 项目类型 '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>

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

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:

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

        }

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2024-10-11 18:23:55

您好,我认为这是因为您的部分视图类型是 System.Web.Mvc.ViewUserControl>
但你选择用户名

select usrs.UserName;

return View(inactive.ToList());

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文