Click here to Skip to main content
11,496,146 members (1,573 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
v3
Comments
Uma Shankar Patel at 24-Dec-13 5:52am
   
Good one
5):
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  
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 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 208
3 Sascha Lefèvre 195
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web04 | 2.8.150520.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