I keep getting this error for my project.
The model item passed into the dictionary is of type
‘System.Collections.Generic.List1[<>f__AnonymousType22[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
You’re passing a list of anonymous objects to your view:
While the view is expecting
IEnumerable<ETMS.Models.DB.tblParent>:You should change your selection to:
in order for the code to work.
Update
Here is how you could use a view model pattern. First, create a view model class, so you’re not passing an anonymous type to your view. Let’s call it
AwesomeEmailViewModel, and it looks like you need.Email,.Usernameand some other properties, so we’ll set those up too.Now, modify your query by using object initialization to populate an instance of
AwesomeEmailViewModelNote: I am guessing which properties belong to which objects (either tblParent or tblParentEmails, so you will need to double-check these
Finally, your view has to know what type(s) to expect, so let’s modify it to expect a list of our newly created AwesomeEmailViewModel instances.
Pay close attention, as I guessed at the namespace as well. In any case, this should give you access to the properties you need inside your view. If you need more, you’ll need to modify the new view model class we created as well as the query in your controller action.