I keep getting this error for my project.
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[<>f__AnonymousType2
2[System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ETMS.Models.DB.tblParent]'.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
==================================================================================
What i want to do is to retrieve the data from the database (one to many) , This is my database table structure
tblparent
- Parent_ID
- Username
- Password
- Firstname
- Lastname
and
tblParentEmail
- ParentEmail_ID
- Email
- Parent_ID
i was made the foreign relation from email to parent, but i could not include with EF while there is another error. i do in this way and caused me this error :
public ActionResult Clientlist()
{
using (ETMSPeopleEntities db = new ETMSPeopleEntities())
{
//var sxc = db.tblParents.Include("tblLocation").Include("tblParentEmails.ParentEmail_ID")
// .OrderByDescending(p => p.Status).ToList();
var members = (from x in db.tblParentEmails
join y in db.tblParents
on x.Parent_ID equals y.Parent_ID
select new { Email = x.ParentEmail, UserName = y.Username }).AsEnumerable();
return View(members.ToList());
}
}
This is my admincontroller
@model IEnumerable<ETMS.Models.DB.tblParent>
@{
ViewBag.Title = "Clientlist";
}
<h2>Clientlist</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Username
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
<td>
@Html.DisplayFor(modelItem => item.Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreateTime)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Parent_ID }) |
@Html.ActionLink("Details", "Details", new { id=item.Parent_ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Parent_ID })
</td>
</tr>
}
</table>
this is clientlist view