Friday 24 February 2012

MVC and IEnumerable Razor woes

Quick one we come across and I have come across before so should be beaten with a large stick for forgetting it. If you have an IEnumerable model type i.e. your declaration looks like :

@model IEnumerable<Spatula>

then you try and loop through this:

@foreach (Spatula s in Model) 

you get an error regarding the model not being the correct type (cannot get IEnumerator from object type)
remember to cast the Model to an IEnumerable like so :

@foreach (Spatula s in (Model as IEnumerable<Spatula>)) 


Presto.  just a little thing if you forget.



No comments:

Post a Comment