Click here to Skip to main content
15,885,757 members
Articles / Web Development / ASP.NET
Tip/Trick

Fixing the MVC Partial View Model Error

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Aug 2012CPOL2 min read 14K   223   1   3
Solving the error "The model item passed into the dictionary is of type 'x', but this dictionary requires a model item of type 'y'.

Introduction

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.

Background

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:

Image 1

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.

Conclusion

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.

History

August 21, 2012 - Initial Version

License

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


Written By
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.

Comments and Discussions

 
Questionissue not fixed Pin
Member 1104302728-Aug-14 3:23
Member 1104302728-Aug-14 3:23 
GeneralMy vote of 5 Pin
Christian Amado21-Aug-12 5:48
professionalChristian Amado21-Aug-12 5:48 
GeneralRe: My vote of 5 Pin
Tim Corey21-Aug-12 6:05
professionalTim Corey21-Aug-12 6: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.