Error when accessing Count property of an IList in ASP.Net MVC 3
When I开发者_如何学编程 try to display the count property of an IList in my ViewModel on the View i get the following error:
The property System.Collections.Generic.IList`1[[WebUI.ViewModels.ItemViewModel, WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Count could not be found.
The line in my view that riases the error is:
<td>@Html.DisplayFor(modelItem => item.ItemCollection.Count)</td>
Upon inspection, the collection is populated correctly and this line of code works correctly:
<td>@Html.DisplayFor(modelItem => item.ItemCollection[0].Id)</td>
I clearly have a reference to System.Collections.Generic so I suspect the error is refering to the WebUI.ViewModels.ItemViewModel object. This is accessible when debugging and the error is only raised when accessing Count.
Why dont you try:
<td>@Model.ItemCollection.Count</td>
Without the HtmlHelper...
You can Cast IList to List then use List.Count
Try this:
<td>@Html.DisplayFor(modelItem => ((List<WebUI.ViewModels.ItemViewModel>)item.ItemCollection).Count)</td>
精彩评论