Smiley Rod
Smiley Rod

Reputation: 53

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

I am trying to render a partial page that includes a table in one of my views in which I have some dropdownlistfor. I keep get the same error and I am not sure how to fix it. Eventually I would like to display values for the tables based on the selection of these 3 dropdowns. Any help would be appreciated.

This is my view

    @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");}

This is my partial view

@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>

This is My controllers

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);
}

Please Help! Thank you!

Upvotes: 0

Views: 41

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You need to pass the object of type which the partial view _Index is strongly typed to which is IEnumerable<IgnitionHub2._0.Models.Car>

You are not passing the model object so the main view is kind of implicitly passing the object of main view with which it is binder.

You need to pass the Cars property of the main model to partial view:

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

Upvotes: 2

Related Questions