Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication5.Models.Employee]', but this dictionary requires a model item of type 'MvcApplication5.Models.Test1'.


I'm getting this error. Below is my code. Please tell me where i'm wrong.


Controller Class

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

namespace MvcApplication5.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        MvcApplication5.Models.TestEntities te = new MvcApplication5.Models.TestEntities();
        public ActionResult Index()
        {
            List<Employee> emp = te.Employees.ToList();

            return View(emp);
        }

    }
}


Model Class

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

namespace MvcApplication5.Models
{
    public class Test1
    {
        public int Code { get; set; }
        public string ID { get; set; }
        public string Name { get; set; }
        public string Department { get; set; }
    }

}



View
C#
@model MvcApplication5.Models.Test1

@{
    ViewBag.Title = "Index";
}

<html>
<body>
<div>
@Html.LabelFor(Model=>Model.ID.ToString())
@Html.LabelFor(Model=>Model.Code.ToString())
@Html.LabelFor(Model=>Model.Name.ToString())
@Html.LabelFor(Model=>Model.Department.ToString())

</div>
</body>
</html>
Posted
Updated 23-Jul-22 2:06am
v3
Comments
uspatel 24-Dec-13 5:52am    
Good one
5):

1 solution

Change the views return model to Employee like below.The Exception is because you are returning the collection of Type Employee but the views expect type Test1. If you want to return a collection to a view you must change the view return type to IEnumerable<type> like below
C#
@model IEnumerable<yournamespace.employee>

@{
    ViewBag.Title = "Index";
}
<html>
<body>
<div>
@foreach(var item in Model){
@Html.LabelFor(item.ID.ToString())
@Html.LabelFor(item.Code.ToString())
@Html.LabelFor(item.Name.ToString())
@Html.LabelFor(item.Department.ToString())
 }
</div>
</body>
</html>
</yournamespace.employee>


If you want to return a particular record in to the view use like below
C#
@model YourNameSpace.Employee
 
@{
    ViewBag.Title = "Index";
}
 
<html>
<body>
<div>
@Html.LabelFor(Model=>Model.ID.ToString())
@Html.LabelFor(Model=>Model.Code.ToString())
@Html.LabelFor(Model=>Model.Name.ToString())
@Html.LabelFor(Model=>Model.Department.ToString())
 
</div>
</body>
</html>


and also you must return Employee type in to that ActionResult not List<employee> like below
C#
public ActionResult Index()
       {
         Employee emp= te.Employees.FirstOrDefault(x=>x.id=1)

           return View(emp);
       }
 
Share this answer
 

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