2

I have the following code:

private Models.mediamanagerEntities dataModel = new Models.mediamanagerEntities();

public ActionResult Index(FormCollection form)
{
    ViewData.Model = (from m in dataModel.Customers select m.Type.Items).ToList();
    return View();
}



View:

%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<Project_Name.Models.Item>>" %>

<% foreach (var m in ViewData.Model)
   { %>

    Title: <%= m.Title %>
    <% } %>

I get the following error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[System.Data.Objects.DataClasses.EntityCollection1[Project_Name.Models.Item]]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[Project_Name.Models.Item].

I'm not too sure what it happening here or how to rectify it, Thanks.

2 Answers 2

1

Your LINQ is creating a list like this List<EntityCollection<Item>>, when your view wants List<Item>.

I don't know query syntax for LINQ very well, but this is how you'd get what you want with function syntax:

ViewData.Model = dataModel.Customers.SelectMany(c => c.Type.Items).ToList();

That will take the many Items under each customer and flatten them into one list of Items.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand function syntax very well. I have tried to enter what you wrote above, but when I get to (c => c.Type.... etc. It is not recognizable, is there something I have to do to enable this? Thank you.
Aha, I figured it out, my brain was being slow.. :) Thank you very much, this worked! I appreciate it.
Make sure you have using System.Linq; at the top of your page.
How would a 'Select Where' statement be formed in Function Syntax? I cant quite figure it out.. thanks.
0

Replace this line:

ViewData.Model = (from m in dataModel.Customers select m.Type.Items).ToList();

with the following:

 ViewData.Model = dataModel.Type.ToList();

1 Comment

This returns me a list of 'Type' and I need a list of Items. I get the following error: ---------- The model item passed into the dictionary is of type 'System.Collections.Generic.List1[Project_Name.Models.Type]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[Project_Name.Models.Item]'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.