Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Here is my Model defined in MVC4 application:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace BindPublicationsData.Models
{
    public class Publication
    {
        public Int64 id { get; set; }
        public string title { get; set; }
        public string abstractInfo { get; set; }
        public string description { get; set; }
        public string authors { get; set; }
        public string rationale { get; set; }
        public string keyTakeways { get; set; }
        public string thumbnail { get; set; }        
    }
}



Below is the Controller class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BindPublicationsData.Controllers
{
    public class PublicationController : Controller
    {       
        private KnowledgeManagerEntities db = new KnowledgeManagerEntities();

        public ActionResult Index()
        {
            var publicationsList = (from eachPub in db.KM_Publication select eachPub).ToList();
            
            return View(db.KM_Publication.ToList());
        }
    }
}


And finally below is my View:

@model IEnumerable<BindPublicationsData.Models.Publication>

@{
    ViewBag.Title = "Index";
}

<h2>Publication</h2>

<table>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.abstractInfo)
        </td>        
    </tr>
}
</table>



After running this application I am getting the following error:

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

Can any one tell me why I am getting this error?
Posted
Updated 5-Jan-20 19:35pm
Comments
[no name] 22-Oct-14 6:53am    
if in the controller you are adding return View(db.KM_Publication.ToList());
then what is the need for the db operation you have done above?
You are returning .ToList type to the view but in view you have used IEnumerable
Nathan Minier 22-Oct-14 7:23am    
Seems likely, but won't change the error.

Change your View to use the concrete type as the model

@model List<BindPublicationsData.Models.Publication>

1 solution

This line:
C#
return View(db.KM_Publication.ToList());

Passes a List element to the view, but in the view you are declared the data (model) a IEnumerable...
HTML
@model IEnumerable<bindpublicationsdata.models.publication></bindpublicationsdata.models.publication>

You have to change one of them...like
HTML
@model List<bindpublicationsdata.models.publication></bindpublicationsdata.models.publication>
 
Share this answer
 
Comments
Richard Deeming 22-Oct-14 9:45am    
Passing IList<T> instead of IEnumerable<T> (for compatible type parameters) wouldn't be a problem.

In this case, the OP is trying to pass a list of KM_Publication objects to something that expects a sequence of Publication objects.
Member 11361539 6-Jul-16 17:02pm    
i understood nothing of what was explained in the answers,please make them more clear. thanks

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900