views:

212

answers:

2

Sorry for the newbie question, but I have the following query that groups parking spaces by their garage, but I can't figure out how to iterate the data in the view. I guess I should strongly type the view but am a newbie and having lots of problems figuring this out. Any help would be appreciated.

    Public Function FindAllSpaces() Implements ISpaceRepository.FindAllSpaces
    Dim query = _
    From s In db.spaces _
    Order By s.name Ascending _
    Group By s.garageid Into spaces = Group _
    Order By garageid Ascending

    Return query

End Function

The controller is taking the query object as is and putting it into the viewdata.model and as stated the view is not currently strongly typed as I haven't been able to figure out how to do this. I have run the query successfully in linqpad.

A: 

Simply have your ViewPage inherit from the generic ViewPage<T> where the T is IEnumerable<Space> (or whatever your model is).

 <%@Page ... Inherits="System.Web.Mvc.ViewPage<IEnumerable<Space>> ... %>
tvanfosson
Okay, did that, and I now get properties showing in intellisense, but not finding the "key" for the group by. Still must be doing something wrong.
Brad Wetli
A: 

If you wish to strongly type the view then the results of your FindAllSpaces needs to be put into an object which is then returned to your view.

So you may have an object called Spaces and it has a property of say type IQueryable and called Spaces.

Then in your view you can itterate through Model.Spaces like foreach(var space in Model.Spaces)

What might be better is that each record that is returned from your query is an object in its own right called Space.

So now your object Spaces has a property called public IQueryable<Space> Spaces {get;set;}

Now in your view you would inherit from <IQueryable<Spaces>> and you can now itterate like this foreach(Space in Model.Spaces).

The final step is to turn each "Space" into a PartialView which inherits from <Space>.

griegs