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.



Thursday 16 February 2012

VS2010 cannot add reference

If you have seen the all too infamous "Cannot add reference" in visual studio and gone down the usual route of making sure you have added the project to the solution, added necessary files, shaking the P.C. and performing a druid ceremony and it still doesn't work, I may have another one for you that I encountered today whilst at work.

At our place, we have a job service solution which is a windows service and a large number of projects which are the individual jobs.  All the settings for configuration are naturally on the service solution e.g. we have a configuration in the job service solution of dev, preprod, release, SIT and UAT.  The job service solution has all the projects for each job and the tests so its pretty large.

We also have solutions which have groups of jobs so we can work on the smaller projects and tests without having to recompile the whole service unless we are deploying the whole thing.  We did a bit of a tidy up so the service solution had the configrations but our other solutions only had debug and release as we didn't need them.

I didn't check the jobservice solutions selected configuration and tried to add an exisitng project and received the all too familiar "cannot add reference" due to the project that was being added having no matching configurtion.

Even worse if you try and add a reference to an existing project that is in the solution which was there before you deleted the configurations you cannot add that either even though its sitting there. What followed was a great deal of head scratching and WTF moments.

If all else fails and you are getting this issue or other bizarre behaviour, check the configurations that each project has against the main solution and you may find there is a bit of a mismatch.