The model item passed into the dictionary is of type ‘[]’ , but this dictionary requires a model item of type ‘[]’

 

I am tired of how many times I found this question on forums. So , if  you encounter this error

The model item passed into the dictionary is of type ‘xxx’, but this dictionary requires a model item of type ‘yyy’

with the variation

The model item passed into the dictionary is of type ‘xxx’, but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[xxx]’

then read on.

ASP.NET MVC is plain and simple. The user enters an url in the browser. A Controller class is made from this request and an Action answer to the url request . The Action   gathers a Model from business logic and gives the Model to the View. The View has a Model also and generates HTML from the Model  .

 

Now let’s get to the code .

The action looks like

public ActionResult MyAction(parameters){

MyModelClassNameFromTheAction modelAction =   // gathers the Model –

return View(modelAction)

}

The View looks like:

@model MyModelClassNameFromTheView

….( html from the MyModelFromTheView )

 

If the 2 class names ( including namespaces) are not the same( and   MyModelClassNameFromTheAction  does not inherit from  , then the error occurs. Practically , MVC is saying : How can I match the 2 classes ?

 

Solving

Usually, you change the Action.

1. You may have inadvertently return the wrong view from the Action. Solution: return View(“~/<folders>/another razor.cshtml”) .

2.  You may have inadvertently return the wrong model . Solution :Solution: return View(another model.) . E.g. the View has as Model  IeNumerable<Model> and in the action you

return Model. The solution is changing

return View(Model)

to

return View( new[](Model));