Je fais quelques modifications sur un système, et j'ai rencontré une exception que j'espérais que quelqu'un pourrait aider avec ?? L'erreur que j'obtiens est la suivante:Erreur MVC: élément de modèle de type 'System.Collections.Generic.IEnumerable`1 requis
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[MyApp.Data.aspnet_Users]'.
Fondamentalement, ce que je veux faire est d'afficher une liste d'utilisateurs inactifs. Voici le contrôleur que j'ai écrit pour cela:
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());
}
}
De là, j'ai ajouté une vue partielle. Voici le code pour cela si quelqu'un est intéressé. Juste la vue habituelle Ajouter -> Liste.
<%@ 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>
Et pour être complet est ici la page d'index où je rends la partie:
<%@ 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 quelqu'un a des conseils, je serais très reconnaissant :)
A travaillé un régal. Merci @Sanja. – 109221793
@TaraWalsh pas de problème, il suffit de demander –