4

I have the following LINQ statement:

var genre = model.ModelGenres.Where(q => q.Active);
var genreWithFeedbackAverage = genre
                .Select(q => new { Genre = q, Average = (q.ModelRate.Interaction + q.ModelRate.Attitude + q.ModelRate.Posing) / 3 })
                .GroupBy(q => q.Genre)
                .Select(q => new { Genre = q.Key, Average = (int)Math.Round(q.Average(p => p.Average)) });
var debugList = genreWithFeedbackAverage.ToList();
this.ViewBag.GenreAverage = genreWithFeedbackAverage.ToList();

In the Razor View, I access the GenreAverage:

var genres = this.ViewBag.GenreAverage;

@foreach (var genre in genres)
{
    <tr>
        <td>
            @genre.Genre.Tag.Name
        </td>
        <td>
            @genre.Average
        </td>
    </tr>
}

However, at line 4, genre.Genre throws the following exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'Genre'

I tried to debug but cannot find the reason for this exception, because the object clearly has it:

enter image description here

How can I fix this problem? Do I have to create a concrete class, and stop using anonnymous class?

P.s: I tried to comment out that line, and as expected, Average is not a valid definition too.

ANSWER: The problem is similar to this topic (you cannot pass an anonnymous type in ViewBag): Stuffing an anonymous type in ViewBag causing model binder issues

I have to create a concrete class:

public class GenreWithAverageViewModel
{

    public ModelGenre Genre { get; set; }
    public int Average { get; set; }

}

and change the LINQ statement:

        var genreWithFeedbackAverage = genre
            .Select(q => new { Genre = q, Average = (q.ModelRate.Interaction + q.ModelRate.Attitude + q.ModelRate.Posing) / 3 })
            .GroupBy(q => q.Genre)
            .Select(q => new GenreWithAverageViewModel { Genre = q.Key, Average = (int)Math.Round(q.Average(p => p.Average)) });
Community
  • 1
  • 1
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Try to change (var genre in genres) to (var genre in genres as IEnumerable) – Andrey Gubal Jun 25 '14 at 15:04
  • @Andrey.Gubal It's an annonnymous class, so it doesnot have a name. I think that is my problem. – Luke Vo Jun 25 '14 at 15:05
  • put the breakpoints in your controller and check if your debugList contains any elements – sylwester Jun 25 '14 at 15:05
  • Does the following solves your problem? http://stackoverflow.com/questions/13800597/object-does-not-contain-a-definition-for-username?rq=1 – Kajal Sinha Jun 25 '14 at 15:08
  • @sylwester I did it, and there are 2 elements in the list. Anyway, the code enters the `foreach` loop means that there is/are elements in the list. The list contains 2 genre elements shown in the picture. – Luke Vo Jun 25 '14 at 15:08
  • 2
    I belive the key is in the exception you marked, look at "type Microsoft.dynamic"... without watching all the error I pressume it's complaining because the object is a dynamic object and with a dynamic object reflection cannot extract properties, so, the easiest solution is to create a concrete class instead of creating an anonymous one on the fly – Gusman Jun 25 '14 at 15:09
  • @DatVM Yes, you are right. May be this answer will help http://stackoverflow.com/questions/5649536/stuffing-an-anonymous-type-in-viewbag-causing-model-binder-issues – Andrey Gubal Jun 25 '14 at 15:09
  • @Andrey.Gubal Thanks, creating a concrete class helps. Sadly, that's the only solution I guess. Thank you, please post it as your answer, and I will mark. – Luke Vo Jun 25 '14 at 15:13

0 Answers0