I am getting below error while calling LINQ queries to join
The model item passed into the dictionary is of type 'System.Linq.Enumerable+<JoinIterator>d__38`4[OnlineApplicationTest.Models.SubCategoryMaster,OnlineApplicationTest.Models.CategoryMaster,System.Int32,<>f__AnonymousType1`3[System.String,System.String,System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[OnlineApplicationTest.Models.SubCategoryMaster]'.
There is no column
CategoryName in
SubCategoryMaster table however it has only CategoryId is present.
I need to display CategoryName to display on view instead of Category.So i have written LINQ query to get the categoryName and passed result to view. However i am getting above error message. Any help on this will be appreciated. Thanks in advance.
What I have tried:
CategoryMaster table
CategoryId
CategoryName etc
SubCategoryMaster table
SubCategoryId
CategoryId
SubCategoryName
OnlineTestEntities2 objOnlineTest = new OnlineTestEntities2();
List<SubCategoryMaster> Subcategories = objOnlineTest.SubCategoryMasters.ToList();
List<CategoryMaster> categories = objOnlineTest.CategoryMasters.ToList();
var innerJoin = (from s in Subcategories
join st in categories
on s.CategoryId equals st.CategoryId
select new
{
SubCategoryName = s.SubCategoryName,
CategoryName = st.CategoryName,
SubCategoryId=s.SubCategoryId
}).AsEnumerable();
return View(innerJoin);
SubCategoryView as below
@model IEnumerable<OnlineApplicationTest.Models.SubCategoryMaster>
@{
ViewBag.Title = "GetAllSubCategory";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>GetAllSubCategory</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CategoryId)
</th>
<th>
@Html.DisplayNameFor(model => model.SubCategoryName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubCategoryName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.SubCategoryId }) |
@Html.ActionLink("Details", "Details", new { id=item.SubCategoryId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.SubCategoryId })
</td>
</tr>
}
</table>