Pregunta

Estoy haciendo algunas modificaciones a un sistema, y ??me he encontrado con una excepción que yo estaba esperando que alguien podría ayudar con ?? El error que estoy recibiendo es la siguiente:

  

Detalles de la excepción:   System.InvalidOperationException: El   elemento de modelo pasa en el diccionario   es de tipo   'System.Collections.Generic.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1 [MyApp.Data.aspnet_Users]'.

Básicamente lo que quiero hacer es mostrar una lista de usuarios inactivos. Aquí es el controlador que he escrito para que:

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

        }

A partir de aquí, entonces, he añadido una vista parcial. Aquí está el código para que si alguien está interesado. Sólo los habituales Agregar Ver -> Lista.

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

completo y si aquí es la página de índice en el que han prestado sus parciales:

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

Si alguien tiene algún consejo Estaría muy agradecido:)

¿Fue útil?

Solución

Hola, creo que es debido a que en el tipo de vista parcial es System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>> pero selecciona los nombres de usuario

select usrs.UserName;

return View(inactive.ToList());

Trate de cambiarlo para seleccionar usrs; pero no usrs.UserName

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top