Click here to Skip to main content
11,702,119 members (64,675 online)
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>
Posted 7-May-13 0:26am
Rockstar_5.7K
Edited 7-May-13 1:29am
vijay__p5.1K
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>

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>

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 Maciej Los 420
1 Sergey Alexandrovich Kryukov 355
2 OriginalGriff 314
3 Mika Wendelius 119
4 CHill60 105
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,507
2 CPallini 4,922
3 Maciej Los 4,371
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 11 Jul 2014
Copyright © CodeProject, 1999-2015
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