I have written a linq query from which I would like to obtain the values from that query and display the results in a table. I keep getting an error that reads as shown below
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType56[System.Int32,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[App.Web.marketingdbclients_invalidEmails]'
When I scaffolded the view from the controller I picked one of my tables as the model and I don't think that is the right way to do it. What is the right type of model to pick if I want to display results from a linq query and or stored procedure because I also have a stored procedure that does the same thing as the linq query so perhaps I could use the sp as an alternative
public class InvalidEmailsController : Controller
{
private MarketingDBEntitiesModel db = new MarketingDBEntitiesModel();
public ActionResult InvalidEmails(int UploadId)
{
//var emails = db.sp_marketing_getInvalidEmailsByUploadId(UploadId);
var emailTable = (from mie in db.marketingdbclients_invalidEmails
join mdt in db.marketingdbclients_dataTable on mie.ClientId equals mdt.ClientId
where mdt.UploadId == 88
select new
{
mie.ClientId, mie.Email1, mie.Email2,
mie.Email3, mie.Email4, mie.DateStamp
});
return View(emailTable);
}
}
This is my view which is bound to a a table as model (which I think is the wrong method)
@model IEnumerable<App.Web.marketingdbclients_invalidEmails>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ClientId)
</th>
<th>
@Html.DisplayNameFor(model => model.Email1)
</th>
<th>
@Html.DisplayNameFor(model => model.Email2)
</th>
<th>
@Html.DisplayNameFor(model => model.Email3)
</th>
<th>
@Html.DisplayNameFor(model => model.Email4)
</th>
<th>
@Html.DisplayNameFor(model => model.DateStamp)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClientId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email3)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email4)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateStamp)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.invalidEmailId }) |
@Html.ActionLink("Details", "Details", new { id=item.invalidEmailId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.invalidEmailId })
</td>
</tr>
}
</table>