Solved

The model item passed into the dictionary is of type 'System.Collections.Generic.List' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumberable'

Posted on 2014-03-18
3
4,496 Views
Last Modified: 2014-03-23
I have an MVC 4 application that has a model developed using EF 5.0 (Code First).  I am investigating TDD and had to make changes to the code generated by the model in order to get my unit tests to work.  Now that my unit tests work I am getting the following error when I try and run the application.

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AppCatalog.Models.AppCatalog_Database]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AppCatalogData.AppCatalog_Database]'.

Open in new window


Here is my controller:
namespace AppCatalog.Controllers
{
    public class DBCatalogController : Controller
    {
        private IAppCatalog db { get; set; }

        public DBCatalogController(IAppCatalog dataContext = null)
        {
            db = dataContext ?? new AppCatalogContainer();
        }
        public DBCatalogController()
        {
            db = new AppCatalogContainer();
        }

        //
        // GET: /DBCatalog/

        public ViewResult Index()
        {
            ViewBag.Title = "Application Catalog -> Database Catalog";

            var appcatalog_database = db.AppCatalog_Database.Include(a => a.AppCatalog_ServerType);
            return View(appcatalog_database.ToList());
        }
   }
}

Open in new window


Here is the model definition in my view:
@model IEnumerable<AppCatalogData.AppCatalog_Database>

Open in new window


Here is my Interface definition:
namespace AppCatalog.Models 
{
    public interface IAppCatalog 
    {
        IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
        IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
        IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
        IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
        IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }

        int SaveChanges();
        void Dispose();
    }

Open in new window


Here is the AppCatalogContainer definition:
namespace AppCatalog.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class AppCatalogContainer : DbContext, IAppCatalog
    {
        public AppCatalogContainer()
            : base("name=AppCatalogContainer")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
        public IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
        public IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
        public IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
        public IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
        public IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
        public IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }
    }
}

Open in new window


I'm not sure what change I need to make to the View or the Controller to get this to work.  Everything worked fine until I made the changes for the unit testing.

Any help is greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 39937343
Well, it looks like you are returning a List() and it is expecting an IEnumerable<> back.

So, I think these lines of code are the issue:

var appcatalog_database = db.AppCatalog_Database.Include(a => a.AppCatalog_ServerType);
return View(appcatalog_database.ToList());

Open in new window


Instead of doing the .ToList(), do as documented here:
http://msdn.microsoft.com/en-us/library/bb739071(v=vs.110).aspx

I would do:

return View(appcatalog_database.IEnumerable<AppCatalogData.AppCatalog_Database>().GetEnumerator();

Open in new window


...or something like that. I don't have your code, so I can't fiddle with it.
0
 

Accepted Solution

by:
dyarosh earned 0 total points
ID: 39937486
I found the problem.  I had to make sure my model derived from DbContext and my interface (IAppCatalog).
0
 

Author Closing Comment

by:dyarosh
ID: 39948428
I found the problem on my own.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to filter by key press ? 6 57
asp.net get gridview datakey from row command 2 18
Selenium and Xpath 4 35
c#, case, if 4 22
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question