2
public ActionResult Index()
{
      var groups = db.SHP_Products
                     .GroupBy(c => c.SHP_Category.Name, 
                                   (category, items) => new 
                                   { 
                                       CategoryName = category, 
                                       ItemCount = items.Count(), 
                                       Items = items 
                                   }
                              );
      ViewBag.group = groups.ToList();
      return View();
}

When running this it will show an error like this:

<ul>
    @foreach (var m in ViewBag.group)
    {
       <h2>@m.CategoryName</h2>
       <a href="#" class="prev1">Previous</a><a href="#" class="next1">Next</a>
       <li></li>
    }
</ul>

'object' does not contain a definition for 'CategoryName'
tereško
  • 58,060
  • 25
  • 98
  • 150
user3110806
  • 21
  • 1
  • 2

5 Answers5

0

You are passing a list of anonymous objects to the View.

Take a look at this answer Dynamic Anonymous type in Razor causes RuntimeBinderException

Community
  • 1
  • 1
Michael Gattuso
  • 13,020
  • 2
  • 25
  • 29
0

i think you are trying to access the <h2>@m.CategoryName</h2> directly, may be you can access it like @m.SHP_Category.Name i don't really know you the sequence on class in your code. try @m.

R K Sharma
  • 845
  • 8
  • 23
  • 42
0

See this answer MVC Razor dynamic model, 'object' does not contain definition for 'PropertyName'

The reason for the error is that the dynamic type created by your GroupBy statement has an access level of "internal", which is not visible to the View. You can rectify by declaring a type or using an Explando - as discussed in this and other answers.

Community
  • 1
  • 1
Rob
  • 1,472
  • 12
  • 24
0

From

The reason for this is that the anonymous type being passed in the controller in internal, so it can only be accessed from within the assembly in which it’s declared. Since views get compiled separately, the dynamic binder complains that it can’t go over that assembly boundary.

One way to solve this is to use System.Dynamic.ExpandoObject.

    public static ExpandoObject ToExpando(this object obj)
    {
        IDictionary<string, object> expandoObject = new ExpandoObject();
        new RouteValueDictionary(obj).ForEach(o => expandoObject.Add(o.Key, o.Value));

        return (ExpandoObject) expandoObject;
    }

Then:

ToExpando(groups); // might need toList() it too.
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
0

Please use ViewData instead of ViewBag here like.

Controller:

public ActionResult Index()
 {
  var groups = db.SHP_Products
                 .GroupBy(c => c.SHP_Category.Name, 
                               (category, items) => new 
                               { 
                                   CategoryName = category, 
                                   ItemCount = items.Count(), 
                                   Items = items 
                               }
                          );
   ViewData["groups"] = groups.ToList();
  return View();
 }

View:

<ul>
@foreach (var m in (dynamic) ViewData["groups"])
{
   <h2>@m.CategoryName</h2>
   <a href="#" class="prev1">Previous</a><a href="#" class="next1">Next</a>
   <li></li>
}