I have a problem with returning data to view. I want to create a structure like category->subcategory and when I trying to run app, dont compiling. I have an error:
cannot convert from ‘System.Collections.Generic.IEnumerable < string > to System.Collections.Generic.IEnumerable < mobile_store.Models.SubCategoryModel >
This is model:
public class CategoryJoinModel
{
public string Category { get; set; }
public List<SubCategoryModel> SubCategoryList {get; set;}
}
second model
public class SubCategoryModel
{
public string SubCategory_Name { get; set; }
}
Controller
public ActionResult Index()
{
var joinedData = from c in db.category
from o in db.sub_category
where c.CAT_ID == o.CATEGORY_CAT_ID
select new
{
CategoryJoin = c.CAT_Name,
SubCatategoryJoin = o.SUBC_Name
};
var gruped = from d in joinedData
group d by d.CategoryJoin
into g select new CategoryJoinModel
{
Category = g.Key,
SubCategoryList = new List<SubCategoryModel>(g.Select(s=>s.SubCatategoryJoin))
};
return View(gruped.ToList());
}
Index View
@model IEnumerable<mobile_store.Models.CategoryJoinModel>
@helper DisplayCat(mobile_store.Models.CategoryJoinModel cat)
{
@Html.DisplayFor(model => cat.Category)
<ul>
@foreach (var item in cat.SubCategoryList)
{
@DisplaySubCat(item)
}
</ul>
}
@helper DisplaySubCat(mobile_store.Models.SubCategoryModel subCat)
{
<li>@MvcHtmlString.Create(subCat.SubCategory_Name)</li>
}
@foreach (var cat in Model)
{
@DisplayCat(cat)
}
@if (Model.Count() == 0)
{
<i>no categories</i>
}
Please help.
The problem is when assigning the
SubCategoryListproperty. It must be aList<SubCategoryModel>: