iteration through DB

Topics: General, Writing modules
Mar 5, 2012 at 3:22 PM

I'm trying to do the equivalent of a MVC 3 'list' based on an existing DB.
With below TR/TD sequence I'm able to display the very last record in th DB.
When running the 'foreach' variation I don't get anything. Also no error!

PS: I'm able to add records as an admin; but added most records in WEB matrix directly - is that ok!

what is the 'right' way to iterate through a DB in Orchard and show all records.

I must miss something very simple here!?
thanks for your time and advise
ed


\view\Part\Eve.cshtml:

<tr>
        <td>
             @Model.Evenement
        </td>
        <td>
            @Model.HomeTeam
        </td>
        <td>
           @Model.Date
        </td>
     </tr>
    
    @*
    @foreach (var item in Model){
    <tr>
        <td>
             @Model.Evenement
        </td>
        <td>
            @Model.HomeTeam
        </td>
        <td>
           @Model.Date
        </td>
     </tr>
    } 
    *@
Mar 5, 2012 at 3:36 PM

Your foreach loop is commented out because it's surrounded with @* ... *@. Can you confirm if that's just a copy/paste error? 

Mar 5, 2012 at 5:12 PM

thanks for reading ..

with the non-commented (activ) part the very last record of the db is shown correctly.

 However, when using the now inactiv (@*  .. *@)  code; I do NOT get anything also NO error msg!
(It's there due to my endless trial and error .....!)
PS: I still think I do not understand the Orchard DB business!

thanks for helping, ed

 

Mar 5, 2012 at 6:09 PM
Edited Mar 5, 2012 at 6:10 PM

If your items are content parts you use IContentManager to get your data. Inject it to your constructor so you can access it later on, and then:

var myItems = _contentManager.Query<MyPart>().List();

This will give you a list of your items, which you can display in your view.

 

There are several examples of this in the Orchard source. The Comments module is one.