如何修复传递到字典中的项目是 'Models' 类型,但字典需要 'System.Collections.Generic.IEnumerable 类型的模型

How can I fix item passed into dictionary is type 'Models', but dictionary requires model of type 'System.Collections.Generic.IEnumerable

我正在尝试在我的一个视图中呈现包含 table 的部分页面,其中我有一些下拉列表。我一直遇到同样的错误,但我不确定如何解决。最后,我想根据这 3 个下拉列表的选择显示 table 的值。任何帮助,将不胜感激。

这是我的看法

    @model IgnitionHub2._0.Models.Car
@{
    ViewBag.Title = "Car Search Page";
}

<h2>Cars</h2>
<div class="center-div">
    <div class="form-inline">
        @Html.DropDownListFor(model => model.CarID, new SelectList(Model.CarList, "CarID", "Model.Name"), "Select Car", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.Model.MakeID, new SelectList(Model.MakeList, "MakeID", "Name"), "Select Make", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.ModelID, new SelectList(Model.ModelList, "ModelID", "Name"), "Select Model", new { @class = "form-control" })
        <button type="submit" id="submit">Submit Search</button>
    </div>
</div>
@{ Html.RenderPartial("_Index");}

这是我的部分观点

@model IEnumerable<IgnitionHub2._0.Models.Car>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Year)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Color)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CarLot.LotName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Year)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Color)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Model.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CarLot.LotName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CarID }) |
            @Html.ActionLink("Details", "Details", new { id=item.CarID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CarID })
        </td>
    </tr>
}

</table>

这是我的控制器

public ActionResult Index()
{

    var cars = db.Cars.Include(c => c.Model).Include(c => c.CarLot);
    var makeList = db.Makes.ToList();
    var modelList = db.Models.ToList();
    var ViewModel = new Car
    {
    CarList = cars,
    MakeList = makeList,
    ModelList= modelList
    };

    return View(ViewModel);
}

public ActionResult _Index()
{
    var cars = new List<Car>(); 
    return PartialView(cars);
}

请帮忙!谢谢!

您需要将部分视图 _Index 强类型化的对象传递给 IEnumerable<IgnitionHub2._0.Models.Car>

您没有传递模型对象,因此主视图有点隐式传递与其绑定的主视图对象。

您需要将主模型的Cars 属性传递给局部视图:

@{ Html.RenderPartial("_Index",Model.CarList);}