ASP.NET MVC ViewBag list of anonymous class throws error on Count() method
ViewBag
is dynamic
, while Count
is an extension method, which isn't supported dynamically (it has to be bound at compile time).
You can either cast to an IEnumerable<dynamic>
:
@if (((IEnumerable<dynamic>)ViewBag.Checkins).Count() > 0)
or use the static method directly:
@if (Enumerable.Count(ViewBag.Checkins) > 0)
Or create a strongly-typed model with a Checkins
property and avoid ViewBag
altogether.
EDIT
Since you're just wanting to check if the count is greater than 0, Any
is more appropriate (and may save some processing time depending on the scenario):
@if (Enumerable.Any(ViewBag.Checkins))