Click here to Skip to main content
13,836,762 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 21 Aug 2012
Licenced CPOL

Fixing the MVC Partial View Model Error

, 21 Aug 2012
Rate this:
Please Sign up or sign in to vote.
Solving the error "The model item passed into the dictionary is of type 'x', but this dictionary requires a model item of type 'y'.


I came across an error statement in MVC that pointed me in the wrong direction. Since it wasn't apparent from the error statement what the true error was, I decided to post my experience here so that others might learn from my experience.


One of the many great features in ASP.NET MVC is the ability to load partial views in a very simple manner. In fact, it is so simple that when one of my coworkers had an issue doing just that, I was initially stumped. Everything looked great but he was getting an InvalidOperationException error that said “The model item passed into the dictionary is of type ‘x’, but this dictionary requires a model item of type ‘y’.” The model for the parent page worked just fine and if we loaded the partial view as a full view (we had two copies of the same view for testing purposes), everything worked.

The Investigation

I was able to recreate the issue in a test project (which I included in the above download link). In my test project, I have a model with First and Last Name and a Phone Number model. The Phone Number model contains the phone number and the description of the number. That is all there is to my data. In my Index view, I display the main model and then have a partial view that renders my phone number model. In the real world, this would be much more complicated but for our purposes, it worked just fine.

In my Home controller, I initialize the main model, pass in a first and last name, and return the View with the strongly-typed model attached. Immediately, I get the same error as my coworker. Here is an image of the error I receive:

Now that I had something to work with, I started playing around with what was happening. First, I know I didn’t pass in phone number information. If I do that, everything works well. However, what if I want the user to add phone number information? Well, it turns out the solution is obvious even if the error statement isn’t: I still need to initialize the Phone Number model. Passing a null value or an uninitialized model will throw the error we see here.


In the end, the solution was simple but the error message sent me down the wrong path. I spent my initial steps looking into why it was getting the wrong model when the real issue was that I was not passing in a valid model.  To solve my issue, I simply initialized the Phone Number model in my main model's constructor method.  When you open the attached solution, these lines are commented out so that you can see the error.  To solve the error, go to the PersonModel.cs file and uncomment the method found there.


August 21, 2012 - Initial Version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tim Corey
Software Developer (Senior) DeGarmo
United States United States
I am currently a Senior Software Developer at a company in Illinois called DeGarmo. My primary skills are in .NET, SQL, JavaScript, and other web technologies although I have worked with PowerShell, C, and Java as well.

In my previous positions, I have worked as a lead developer, professor and IT Director. As such, I have been able to develop software on a number of different types of systems and I have learned how to correctly oversee the overall direction of technology for an organization. I've developed applications for everything from machine automation to complete ERP systems.

I enjoy taking hard subjects and making them easy to understand for people unfamiliar with the topic.

You may also be interested in...

Comments and Discussions

Questionissue not fixed Pin
Member 1104302728-Aug-14 4:23
memberMember 1104302728-Aug-14 4:23 
GeneralMy vote of 5 Pin
Christian Amado21-Aug-12 6:48
professionalChristian Amado21-Aug-12 6:48 
GeneralRe: My vote of 5 Pin
Tim Corey21-Aug-12 7:05
professionalTim Corey21-Aug-12 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 21 Aug 2012
Article Copyright 2012 by Tim Corey
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid