Michael Scofield
Michael Scofield

Reputation: 25

How to pass Anonymous type result in a viewbag and Render the same in Razor view

I'm trying to find out the book publications count in each Department and want to render the same in view. But i'm getting error like

'object' does not contain a definition for 'Key' Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Key'

Source Error: 
Line 20:         <tr>
Line 21:             <td>
Line 22:                @item.Key
Line 23:                 
Line 24:             </td>

here is the Controller code

 public ActionResult BooksCount()
    {
    ViewBag.booksGroup= db.BookPublications.GroupBy(x => x.FacultyDepartment)
           .Select(y => new { FacultyDepartment = y.Key, DepartmentCount = y.Count() });


        return View();
    }

Here is my View

   @model Iam.Models.BookPublication
<h2>Books Count</h2>
<table>
    <tr>
        <th>
            Department
        </th>
        <th>
            Count
        </th>
    </tr>
    @foreach(var item in ViewBag.booksGroup)
    {
        <tr>
            <td>
               @item.Key
                
            </td>
            <td>
                @item.Count()
                
            </td>
        </tr>

    }
</table>

Upvotes: 1

Views: 263

Answers (2)

AlexiAmni
AlexiAmni

Reputation: 402

Correct property name for access is

FacultyDepartment

not the

key

 @model Iam.Models.BookPublication
<h2>Books Count</h2>
<table>
    <tr>
        <th>
            Department
        </th>
        <th>
            Count
        </th>
    </tr>
    @foreach(var item in ViewBag.booksGroup)
    {
        <tr>
            <td>
               @item.FacultyDepartment

            </td>
            <td>
                @item.DepartmentCount 

            </td>
        </tr>

    }
</table>

Upvotes: 2

AlexMiamorsch
AlexMiamorsch

Reputation: 53

I ran into a similar issue building an ASP.NET app with the Entity Framework - it wouldn't accept anonymous types, so I went ahead and built a ViewModel class.

In a nutshell, it is just a plain and simple class with properties, but the type definition allows you to pass it to the ViewBag, assuming your serializer in the background does its job.

So create a little class like

public class BookPublicationViewModel
{
   public FacultyDepartment { get; set; }
   public DepartmentCount   { get; set; }
}

And change your LINQ Statement to this:

 public ActionResult BooksCount()
{
ViewBag.booksGroup= db.BookPublications.GroupBy(x => x.FacultyDepartment)
       .Select(y => new BookPublicationViewModel{ FacultyDepartment = y.Key, DepartmentCount = y.Count() });


    return View();
}

Edit:

Line 20:         <tr>
Line 21:             <td>
Line 22:                @item.Key
Line 23:                 
Line 24:             </td>

Should of course be changed to the actual name of the property you assigned in the select statement, e.g. @Item.Key -> @item.FacultyDepartment

Line 20:         <tr>
Line 21:             <td>
Line 22:                @item.FacultyDepartment
Line 23:                 
Line 24:             </td>

As @Alexi already mentioned.

EDIT: @Pratik mentioned that razor actually DOES accept anonymous types - not sure why it wouldn't work on my end. In this case, changing your property names in the View to match them should be enough to fix your issue.

Upvotes: 0

Related Questions