【问题标题】:The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable? [duplicate]传入字典的模型项的类型为“ ”,但此字典需要类型为“System.Collections.Generic.IEnumerable?”的模型项[复制]
【发布时间】:2012-06-25 00:33:36
【问题描述】:

执行应用程序后,我得到 “传入字典的模型项的类型为“ClubStarterKit.Web.ViewData.Forum.NewMessageListViewData”,但该字典需要一个“System.Collections”类型的模型项。 Generic.IEnumerable`1[ClubStarterKit.Web.ViewData.Forum.NewMessageListViewData]'。

鉴于我有以下代码

    <%if ( User.Identity.IsAuthenticated) { %>

        <div class="fullwidth" id="message-add-block">
          <%using (Ajax.AsyncForm(Website.Shared.Views.DisplayTemplates.NewForumMessageList, new AsyncFormOptions(AsyncFormType.TargetUpdate, FormMethod.Post, "newMessages", targetUpdate: "messages", elementBlockId: "message-add-block", postRequestFunction: "afterMessageUpdate")))
          {%>
                <h3>Add New Message Final Test</h3>
                <br />
                <%: Html.Wysiwyg("message") %>
                <br />
               <%-- <input type="hidden" value="<%: Model.ThreadSlug %>" name="thread" />--%>
                <input type="hidden" value="-1" name="messageId" />
                <input type="submit" value="Add Message4 final" />
          <%}%>




          <br />
          <a id="newmessage" href="#">New message4 final</a>
        </div>

          <%}%>

在控制器中代码是

 public virtual ActionResult Index()
    {
        ViewData.Title("New Message");
        return View(Views.List, new ClubStarterKit.Web.Infrastructure.Forum.NewMessageListAction().Execute());
    }

在下面的截图中是:

传入字典的模型项的类型为“Myapplication.Web.ViewData.application.NewMessageListViewData”,但此字典需要类型为“System.Collections.Generic.IEnumerable”的模型项

请告知哪个代码区域有问题

【问题讨论】:

  • NewMessageListAction().Execute()的返回类型是什么?
  • 发生错误是因为您试图将单个对象传递给需要集合的视图。

标签: c# asp.net asp.net-mvc


【解决方案1】:

您正在向视图传递单个 NewMessageListViewData,但它已被强类型化为仅接受实现 IEnumerable&lt;T&gt; 的对象,其中 T 是 NewMessageListViewData。例如,List&lt;NewMessageListViewData&gt; 可以工作。

如 cmets 中所述,我将首先查看 ClubStarterKit.Web.Infrastructure.Forum.NewMessageListAction.Execute() 的返回类型,或者,如果您只打算在此视图中显示 NewMessageListViewData 的单个实例,您需要在视图中设置模型以反映这一点。

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 2022-01-16
    • 2015-09-19
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多