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?