Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Please provide any solution. Below is the scenario:

Controller
public ActionResult Category(int id)
 {
  var result = db.Categories.Where(s => s.DepartmentId == id).Select(x => new  { id = x.CategoryId, name = x.CategoryName }).ToList();       
   return View(result);
}


View
@model IEnumerable<kshoppingcart.models.category>


Model

namespace KShoppingCart.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Category
    {
        public Category()
        {
            this.SubCategories = new HashSet<SubCategory>();
        }

        public Nullable<int> DepartmentId { get; set; }
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }

        public virtual Department Department { get; set; }
        public virtual ICollection<SubCategory> SubCategories { get; set; }
    }
}

Ex: Here I can get count=2 with values in variable result. But how can I display these values using View.

Thanks
JSagar
Posted
Updated 1-Jan-15 0:20am
v3
Comments
[no name] 2-Jan-15 2:44am    
Hello Sagar,
The problem here is you are selecting the ID and Name in the LINQ. Please remove the select statement. Just use where(condition).ToList();
Please check using this and then follow as mentioned in the solution.
jsagar05 2-Jan-15 5:37am    
Hi Suraj,
As per your suggestion I removed select statement and it worked.
Thanks a lot

Use
C#
@foreach(var item in Model.SubCategories)
{
    <span>@item.XYZ</span>
    //...Continues.. for other properties.
}

I hope I have anticipated correct.
Please post back queries if any.
thanks.:)
 
Share this answer
 
v2
Comments
Sanket Saxena 1-Jan-15 7:04am    
Should work :)
In view You can wright follwing code 

HTML
<table>
<tr>
<th> Category Name </th>
<th> Department </th>
<th> More (can be blank) </th>
</tr>


@if(Model!=null)
{
HTML
<pre lang="c#">
foreach(catItem in Model)
{
<tr>
<Th> @catItem.CategoryName </Th>
<Th> @catItem.Department </Th>
<Th> Link for expend collapse </Th>

if(catItem.SubCategories!=null)
{
foreach(subcatItem in catItem.SubCategories)
{
<tr>
<Th> col name </Th>
........
}
}

</Tr>
}
}
HTML

 
Share this answer
 
v2
Comments
jsagar05 2-Jan-15 2:33am    
Actually in my View I am passing:
@model IList<kshoppingcart.models.category>

but I am getting below error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType3`2[System.Int32,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[KShoppingCart.Models.Category]'.

Please refer my above code also.

Thanks
Manoj Kumar Choubey 2-Jan-15 3:14am    
You should create view model.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900