传递到字典中的模型项是类型'';,但是这个字典需要一个类型为';System.Collections.

本文关键字:类型 字典 一个 System Collections 模型 | 更新日期: 2023-09-27 18:26:01

我试图创建一个部分视图来显示项目列表,但一直遇到这个错误,

传递到字典中的模型项的类型为"Revision.Models.GroupEvent",但此字典需要类型为"System.Collections.Generic.GroupIEnumerable `1[Revision.Models.Event]"的模型项。

这是我的家庭控制器

public class HomeController : Controller
{
    public RevisionDb db = new RevisionDb();
    EventViewModels evm = new EventViewModels();
    public ActionResult Index()
    {
        evm.Events = db.GroupEvents.ToList();
        return View(new GroupEvent() { EventPartialModel = evm.Events.ToList()} );
    }
}

这是我使用的视图模型

public class EventViewModels
{
    public int NumberofEvents { get; set; }
    public List<GroupEvent> Events { get; set; }
    public int EventCount { get; set; }
}

这是我的部分视图

@model IEnumerable<Revision.Models.GroupEvent>
@using Revision.Models
@if (Model != null)
{
    <div class="grid">
        <table cellspacing="0" width="80%">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td align="left">@item.EventTitle</td>
                        <td align="left">@item.EventDesc</td>
                        <td align="left">@item.EventDate</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

这是我的主要观点

@model IEnumerable<Revision.Models.GroupEvent>
@{
    ViewBag.Title = "Index";
}
<a href="~/Models/GroupEvents.cs">~/Models/GroupEvents.cs</a>
<div>
    @Html.Partial("EventsPartialView", Model)
</div>

传递到字典中的模型项是类型'';,但是这个字典需要一个类型为';System.Collections.

return View()的参数是new GroupEvent。它应该是一个IEnumerable<GroupEvent>。然后你说你正在使用的ViewModel是EventViewModels,它没有被引用。

看起来应该是

主要类别:

return View(new EventViewModels{Events = evm.Events.ToList()})

MainView

@model EventViewModels

@Html.Partial("EventsPartialView", Model.Events)