views:

68

answers:

1

Hi,

I am getting an error saying:

The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery`1[WebUI.Models.MailListViewModel]', but this dictionary requires a model item of type 'WebUI.Models.MailListViewModel'.

This is in my controller:

public ViewResult List()
    { var mailToShow = from m in mailRepository.Mail
                         join p in profilesRepository.Profiles on m.SenderId equals p.ProfileId 
                         select new MailListViewModel
                                    {
                                        SenderId = m.SenderId,
                                        ProfileId = p.ProfileId,
                                        UserName = p.UserName,
                                        Subject = m.Subject
                                    }; 
       return View(mailToShow);
    }

This is in my MailListViewModel:

public class MailListViewModel
{        
    public int SenderId;
    public int ProfileId;
    public string UserName;
    public string Subject;
}

This is in my view:

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

<% foreach (var m in (IEnumerable)Model)
       { %>
    <tr>
        <td>
        </td>
        <td>TODO image</td>   
        <td>
        <%: Model.SenderId %></td>

        <td><%: Model.Subject%></td>
    </tr>
    <% } %>

The error message seems to be conflicting, saying that I am using the correct model.

What should I do?

A: 

Your query is returning an IQueryable<MailListViewModel>, essentially one MailListViewModel for every result in the query. Your view is expecting only a single MailListViewModel.

You should update your view to expect IEnumerable<MailListViewModel>, like so:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IEnumerable<WebUI.Models.MailListViewModel>>" %>

Also, once you've done that you shouldn't cast the Model to IEnumerable as that will result in each item in the enumeration being treated as type object so it will appear as though none of your properties exist.

Dave
Great, almost working now!After I updated the view, I got the message:CS1061: 'object' does not contain a definition for 'Subject' and no extension method 'Subject' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)It was highlighting:<%: m.Subject %>Intellisense said:Cannot resolve symbol 'Subject'What should I try next?
Castielle
If you're still casting your model to IEnumerable it's probably that. I've updated my answer accordingly.
Dave
Closer :) Now I'm getting an error saying:"The query contains references to items defined on a different data context."and it is referring to the line:"<% foreach (var m in Model)"
Castielle
You can't join across two data contexts. You have to either just use one or convert to a (filtered) list (.ToList()) and then join the lists as that will use LinqToObjects.
Dave
Thanks. I'm going to try two different queries for now and see if that works, but if not, I'll try the List solution.
Castielle
I ended up combining the two data contexts into one and now it's working.
Castielle