4

我正在對系統進行一些修改,並且遇到了一個異常,我希望有人可以幫忙?我得到的錯誤如下:MVC錯誤:類型'System.Collections.Generic.IEnumerable`1需要的模型項目

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]'.

基本上我想要做的是顯示一個非活動用戶列表。這裏是我寫的控制器:

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> 

如果任何人有任何意見,我會非常感激:)

回答

3

嗨,我認爲它是因爲在您的局部視圖類型爲System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>> 但你選擇的用戶名

select usrs.UserName; 

return View(inactive.ToList()); 

試着改變它來選擇usrs;但不包括usrs.UserName

+0

工作的一種享受。謝謝@Sanja。 – 109221793 2010-11-30 12:23:19

相關問題