Search code examples
asp.netasp.net-mvc-4razor-2

ICollection Count method fails in ASP.NET MVC 4


I have a ICollection of Projects in my user class

public ICollection<Project> Projects { get; set; }

When I try to render the count of projects in my view, it gives an error

<h2>You have @Model.Projects.Count() projects....</h2>

Any help appreciated.


Solution

  • ICollection doesn't have a Count method, it has a Count property. You are probably getting confused with the LINQ Count extension method which is supported on an IEnumerable interface.

    Just remove the parenthesis at the end of the Count call i.e.

    <h2>You have @Model.Projects.Count projects...</h2>