1

I'm trying to use this in the controller to get group all Category together and Count the total of each Category.

        //Report
        public ActionResult Report()
        {
            var reports =
         db.Product
         .GroupBy(r => r.CategoryName)
         .Select(r => new { Name = r.Key, Count = r.Count() });
            return View(reports);
        }

How to access and display the Count and CategoryName in the View? I want to display it like this:

CategoryName     Count
P1                 2
P2                 3
P3                 4
P4                 2

I have something like this in the Report.cshtml but it's not working:

@model IEnumerable<SiteKitBMI.Models.Product>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Count)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Count)
        </td>
    </tr>
}

</table>

Can anyone shine some light onto this?? Any help is much appreciated.

4
  • 1
    how is it not working? error? empty screen? Commented Dec 19, 2016 at 11:21
  • Your not returning IEnumerable<Product> to the view (your code would be throwing an exception). Create a view model (say ProductVM)containing properties Name and Count and project your query into it, - select r => new ProductVM { ... } and then it to a view with @model IEnumerable<ProductVM> Commented Dec 19, 2016 at 11:22
  • First you need to create the list and use view bag to send it on view and then get it by type casting it then you can use the same in the foreach loop . Commented Dec 19, 2016 at 11:41
  • Can you show us what you expecting and what is currently showing ? Commented Dec 19, 2016 at 13:03

1 Answer 1

1

Your controller query is returning a collection of anonymous objects to a view which expects a collection of Product and will there for throw a The model item passed into the dictionary is of type ... but this dictionary requires a model item of type ... exception.

Since Product does not contain a property Count, you need to create a view model to represent what you want to display in the view

public class ProductQuantitiesVM
{
    public string Name { get; set; }
    public int Count { get; set; }
}

and then project your query to a collection of ProductQuantitiesVM

var reports = db.Product.GroupBy(r => r.CategoryName)
    .Select(r => new ProductQuantitiesVM()
    {
        Name = r.Key,
        Count = r.Count()
    });
return View(reports);

and finally, change the view to use ProductQuantitiesVM

@model IEnumerable<ProductQuantitiesVM>
....
Sign up to request clarification or add additional context in comments.

2 Comments

I orginally have issue doing toList() from iqueryable with error saying it's not supported. So I changed it around a little. The main underlying is with the CategoryName property containing conditional statements which relies on values from another property.
But yeah, I've now used the ProductQuantitiesVM view model class approach and fixed that up and it's working perfectly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.