Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET MVC
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
 
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
 
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
@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 7-May-13 1:26am
Rockstar_5.7K
Edited 7-May-13 2:29am
vijay__p4.6K
v3
Comments
Uma Shankar Patel at 24-Dec-13 5:52am
   
Good one
5):
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 like below
@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
@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 like below
public ActionResult Index()
       {
         Employee emp= te.Employees.FirstOrDefault(x=>x.id=1)
 
           return View(emp);
       }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is siruxgh
<pre lang="c#"><pre lang="c#"><pre lang="c#">
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 11 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100