Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I used data base apprach entity framework. after creating database and edmx class.
i got the model class like follow.
class name : Employee.cs

namespace DropShipTestEmployee
{
    using System;
    using System.Collections.Generic;

    public partial class Employee
    {
        public int Id { get; set; }
        public string descript{ get; set; }
        public string grp{ get; set; }
        public decimal price{ get; set; }
    public string garment { get; set; }
        public string sku { get; set; }
        public string upc2 { get; set; }
        public string user4 { get; set; }
        public string carelabel { get; set; }
        public string a_sku1 { get; set; }
        public string label1 { get; set; }
        public string label2 { get; set; }
        public string label3 { get; set; }
        public string userid { get; set; }
    }
}

index.cshtml
@model IEnumerable<DropShipTest.Employee>
@{
    ViewBag.Title = "Home page";
}


<div class="row">
    <div class="col-md-4">
        <h2>Categories</h2>
        <img src="~/Image/Testimg/te.jpg" class="img-responsive" />
    </div>
    <div class="col-md-8">
        <h2>Popular Products</h2>
        @foreach (var item1 in Model )
        {



            <div class="col-md-3">
              <img src="~/Images/Testimg/sil.jpg" />
                @item1.id<br />
                @item1.descript<br />
                @item1.grp<br />
                @item1.price<br />


            </div>
        }

    </div>

</div>
then controller class and action method index

if i did select particular column from table like below, it works fine. no error:

        WFTOOLSDBContext3 dbcontext = new WFTOOLSDBContext3();
        public ActionResult Index()
        {

       var ivtfval = dbcontext.Employees.Take(16).ToList();
        return View(ivtfval);
        }

But that Employee has more than 100 column. so in my project i do not want to select all column whereas i need to select just few column from table.
for that i used linq query like follow.

WFTOOLSDBContext3 dbcontext = new WFTOOLSDBContext3();
        public ActionResult Index()
        {

           var ivtfval = (from a in dbcontext.Employees
                           select new { a.id, a.descript, a.grp, a.price }).Take(16);
            return View(ivtfval.ToList());

        }

if i run, I am getting this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType4`4[System.Int32,System.String,System.String,System.Nullable`1[System.Decimal]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[DropShipTest.Employee]'.
Posted

1 solution

1.Your view is waiting for a Model of type "list" of Employee and you are sending to it a "list" of Anonymous type created by your LINQ selection.

2.The solution is to create a new class named EmployeeModel class that will have only those properties that are needed into your view; then you should change your view to be a "list" of this EmployeeModel. This class can be used directly in your LINQ expression above to create the model for your view like below:
C#
public class EmployeeModel
{
     public int ID{get;set;}
     public string Description{get;set;}
     public int Group{ get; set;}
     public double Price{get;set;} 
}

//...

public ActionResult Index()
        {

           var ivtfval = (from a in dbcontext.Employees
                           select new EmployeeModel { ID= a.id, Desciprion = a.descript, Group =a.grp, Price = a.price }).Take(16);
            return View(ivtfval.ToList());

        }
 
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