Tuesday, July 15, 2014

Dictionary requires a model item of type System.Collections.Generic.IEnumerable in MVC 3

MVC

I encountered an error while implementation of return multiple models to single view.

Kindly visit an article for reference.

Return Multiple Models in single View in MVC3

An error was occurring on routine basis.Initially I found challenge to find the root cause of this.

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

In this blog I am sharing the way to resolve this error.

Whenever I run my application and paste the following URL in browser http://localhost:60346/Home/rsvpform , it prompts me an 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. “as I’ve set below for sample app.

clip_image001

Now figure out the reason for occurrence of an error, it generates an error when we are returning only of base type model class object as an example below:

clip_image002

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

If I uncomment the above three lines as depict also in below image it will run as expected, because of this is we are returing viewModelList object as Enumerable type .

clip_image003

More Summarized words are, Model declaration in View and the model being return from Controller class must be sync with each other.

clip_image005

 

To know more about MVC kindly go through with the links given below:

· Smart Working With Custom Value Providers in ASP.Net MVC

· Invoke Action With Model Binders in MVC

· Extension Helpers Method in MVC

· Custom Button With TagBuilder Using MVC Razor Engine

· Precompiled Razor View Using RazorGenerator MVC and PreCompiledViewEngine in MVC 4

· RETURN MULTIPLE MODELS IN SINGLE VIEW IN MVC3

· CALL CONTROLLER ACTION METHOD FROM JQUERY USING AJAX

· EXECUTION ORDER OF FILTERS IN MVC 4 WITH PRACTICES: IMPORTANT FAQ

· MEANING OF SCAFFOLDING IN MVC

· REMOVE AMBIGUTY OF CONTROLLER NAMES IN MVC APPLICATION

· CUSTOM BUTTON WITH TAGBUILDER TECHNIQUE USING MVC RAZOR ENGINE

· CONVERSION HELPERS IN MVC RAZOR: VALIDATING POSTED DATA

Thanks

Enjoy Coding and Stay Happy Smile

1 comment :