Dictionary Requires a Model Item of Type System.Collections.Generic.IEnumerable in MVC 3

I encountered an error in the implementation of returning multiple models to a single view.

Kindly visit the following article for reference:

Return Multiple Models in single View in MVC3

An error was occurring on a routine basis. Initially it was a challenge to diagnose the root cause of this.

Later I understood an error and changed my approach to overcome this.

In this article I am sharing how to resolve this error.

Whenever I run my application and paste the URL "http://localhost:60346/Home/rsvpform" in a browser, it gives me the error:

The model item passed into the dictionary is of type 'MVCSample.Models.ParentModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MVCSample.Models.ParentModel]'.

The reason behind the View "(.cshtml) expects the IEnumerable model type" is the I've set the following for the sample app:

MVC1.jpg

Now to help diagnose why the error occurs, it generates an error when we are returning only a base type model class object as in the following example:

MVC2.jpg

So the point of interest is that whenever you return a model type object it must be synchronized with the view model type.

If I uncomment the preceding three lines as shown, also in the following image, it will run as expected, because of this is we are returning the viewModelList object as an Enumerable type.

MVC3.jpg

To say it more simply, Model declaration in the View and the model being returned from the Controller class must be synchronized with each other.

MVC4.jpg

A sample application is attached as a reference.

Enjoy Coding and Stay Happy.

Up Next
    Ebook Download
    View all
    Learn
    View all