5

I’m new to using both Entity Framework and MVC, but not new to programming. I am working with a large table and I want to return selected columns of the query back to the view and list them. The basic query is as follows:

public class EmployeeController : Controller
{
    private ADPEntities db = new ADPEntities();

    // GET: /Employee/
    public ActionResult Index()
    {
        var tblEmployeeADPs = db.tblEmployeeADPs
            .Where(p => p.Status == "Active")
            .Select(p => new UserViewModel.USerViewModelADP
            {
                Status = p.Status,
                FirstName = p.FirstName,
                LastName = p.LastName,
                SSN = p.SSN
            });

        return View(tblEmployeeADPs.ToList());
    }
}

I created a basic C# class to strongly type the results (i.e. UserViewModel) and I still get an error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[UserViewModel.USerViewMode‌​lADP]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[DysonADPTest.Models‌​.tblEmployeeADP]'.

when I execute the page.

I’m not sure what I’m missing, as I was sure that (cobbling what I’ve read) that this would be the answer.

2 Answers2

2

Please try the following in your view:

@model IEnumerable<DysonADPTest.UserViewModel.USerViewModelADP>

Your problem lies in using the .Select() method which changes the type your controller action is returning from

IEnumerable<DysonADPTest.Models.tblEmployeeADP>

which your view is also expecting to something entirely different. For this to work, the type your controller action returns and your view uses should match. It's either not use the .Select() method in your controller action or change the type your view uses.

Patee Gutee
  • 292
  • 2
  • 19
  • Oddly enough, I get the same error, which leads me to believe it's not related to using the .ToList Method. Here's what shows: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[UserViewModel.USerViewModelADP]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[DysonADPTest.Models.tblEmployeeADP]' – Thomas Washington Mar 22 '17 at 16:08
  • @ThomasWashington Answer edited. – Patee Gutee Mar 22 '17 at 16:19
  • The .Where clause works perfectly (always has). It's the select statement, when I try to narrow the column list, that I have problems. – Thomas Washington Mar 22 '17 at 16:29
  • @ThomasWashington Firstly, you are expecting `@model IEnumerable` in your view. You need to use your ViewModel instead. Secondly, you might also want to use automapper or expressmapper to convert your domain model to view model. – Patee Gutee Mar 22 '17 at 16:48
  • Patee. Thanks. I auto generated the models within the project and the basic scaffolding features to generate the pages, so I'm not sure how they could be out of sync. I should also point out that the auto generated code works if I let it return all of the columns. So, I'm thinking that the problem lies elsewhere. – Thomas Washington Mar 22 '17 at 17:07
0

You are creating a list with a new object type (USerViewMode‌​lADP)

You can filter, but just keep the same type (an objects)

public ActionResult Index()
{
    var tblEmployeeADPs = db.tblEmployeeADPs
        .Where(p => p.Status == "Active")
        .Select(p => p)

    return View(tblEmployeeADPs.ToList());
}
Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
  • Thanks for the suggestion. When I do that, I get a similar error: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[DysonADPTest.Models.tblEmployeeADP]'. – Thomas Washington Mar 22 '17 at 16:26
  • A syntax that made sense to me was to preface that .Select statement with "New" to sort of (in my mind) override the default resultset (which is everything). Sadly, I get the same iEnumberable error. – Thomas Washington Mar 22 '17 at 16:41
  • Example: .Select(p => new { p.FirstName, p.LastName, p.SSN, p.Status, }); – Thomas Washington Mar 22 '17 at 16:41
  • That way you will be creating a list of anonymous type – Eduardo Molteni Mar 22 '17 at 18:10