1

I have an edit view on my MVC web app that has some partial views in it. The purpose of the partial views is to allow a user to add data to a dropdown list that is perhaps missing. The partial views work on the create view but not the edit view. When you try to edit a car the parial view throws an error of:

The model item passed into the dictionary is of type 'MyProject.Models.Car', but this dictionary requires a model item of type 'MyProject.Models.Manufacturer'.

Here is a snapshot of my code. I'm using Ajax forms for everything I do here.

Edit

@model MyProject.Models.Car

@using (Ajax.BeginForm("Edit", "Car", new AjaxOptions { 
    HttpMethod = "POST", 
    OnSuccess = "success" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    ...form fields etc
}
@Html.Partial("_Manufacturer")

_Manufacturer (partial view)

@model MyProject.Models.Manufacturer
 @using (Ajax.BeginForm("Create", "Manufacturer", new AjaxOptions { 
    HttpMethod = "POST", 
    OnSuccess = "success" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()

    ...form fields etc
}

As you can see, the edit view allows you to edit a car whilst the partial view, which is triggered as a modal (bootstrap 4.0), allows you to add manufacturers to one of the dropdown lists should they be missing.

I'm not sure as to why this causes a dictionary error though, the Manufacturer model is defined in the partial view and so shouldn't be using the Car model. Is this related to the fact we are editing a specific record and therefore pass an ID as a parameter?

Yanayaya
  • 2,044
  • 5
  • 30
  • 67
  • 2
    `@Html.Partial("_Manufacturer")` does not pass a model as a parameter, the context of the view that calls this partial has a model of `MyProject.Models.Car` so that is what will be passed to the Partial. If you want the partial to have a different model, pass whatever model you want in the Partial call – maccettura May 15 '18 at 15:12
  • 1
    The partial view is expecting an instance of a `Manufacturer` object, but you're not providing it one: `@Html.Partial("_Manufacturer")` Is there a property on your `Car` object which is of type `Manufacturer` that you should be supplying? – David May 15 '18 at 15:12

1 Answers1

3

When you call your partial:

@Html.Partial("_Manufacturer")

You are not passing it a model. This means it will implicitly pass whatever the model of the calling view is, in your case that is a MyProject.Models.Car.

What you need to do is add the model to the @Html.Partial() call:

//Assuming your Car class has a Manufacturer property
@Html.Partial("_Manufacturer", Model.Manufacturer)
maccettura
  • 10,514
  • 3
  • 28
  • 35
  • This may be where my confusion is. The partial view is supposed to use the `Models.Manufacturer` model for its data because it's just a form to update a different table. The `manufacturer` is not in the `Cars` table. – Yanayaya May 15 '18 at 15:52
  • @Yanayaya This is where you should create a view model that will have a Cars instance _and_ a Manufacturer instance. It is not often that an MVC app is _strictly_ MVC. Most of the time a ViewModel is required so that multiple classes/models can be handled in one View/UI – maccettura May 15 '18 at 15:53