I am running in to a problem where I have made the following query in my controller:
var query = from pmt in db.ProjectHasTags
join project in db.Projects on pmt.ProjectId equals project.ID
join tag in db.ProjectTags
on pmt.TagId equals tag.ID
group pmt by pmt.Project into pmtGroup
select new
{
Project = pmtGroup.Key,
Tags = pmtGroup.Select(project => project.ProjectTag)
};
I want to return this query to a view using:
return View(query.ToList());
In the view file I have the following code:
@model IEnumerable<portfolio.Models.ProjectHasTag>
@foreach (var p in Model)
{
@p.Project.Title
foreach (var tag in p.Tags)
{
@tag.title
}
}
I get the following error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[<>f__AnonymousType6
2[portfolio.Models.Project,System.Collections.Generic.IEnumerable1[portfolio.Models.ProjectTag]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[portfolio.Models.ProjectHasTag]'.
The ProjectHasTag model code:
public class ProjectHasTag
{
public int ID { get; set; }
public int? ProjectId { get; set; }
[ForeignKey("ProjectId")]
[DisplayName("Project")]
public virtual Project Project { get; set; }
public int? TagId { get; set; }
[ForeignKey("TagId")]
[DisplayName("Tag")]
public virtual ProjectTag ProjectTag { get; set; }
public virtual ICollection<ProjectTag> Tags { get; set; }
}
This is what I want to achieve: https://i.stack.imgur.com/DAZ5n.png (I cant post images yet)
thanks for taking the time, English is not my first language.