How to iterate thru collection in model to an unordered list?

175 views Asked by At

I'm using a UI Pattern Framework. The Framework allows me to access data in the form of a Model in the client (similar to MVC)<%# Model.Widget %>. I'm binding to a FormView control using Eval statements. Trying to figure out how to iterate thru a collection in the model to an unordered list.

If one of the properties of widget is a string array, how can I iterate thru the property for the widget item? This is what I've got so far:

<ul style="list-style-type: none; margin-left: 0px">
    <% var services = Eval("Services") as List<string>; // Getting exception here
        foreach (var service in services)
        { %>
            <li><%= service %></li>    
     <% } %>
</ul>

Obviously, this is wrong. I'm getting an InvalidOperationException cause I am databinding to variable with the Eval instead of an item in the ItemTemplate of the control.

Thanks in advance!

2

There are 2 answers

0
Rich On BEST ANSWER

Here is how I got it work using a Repeater Control.

<ul>
    <asp:Repeater ID="rptServices" runat="server" 
        DataSource='<%# Eval("Services") %>'>
             <ItemTemplate>
                  <li><%# Container.DataItem %></li>        
             </ItemTemplate>
    </asp:Repeater>
</ul>

Assign the property as a DataSource using the Eval method of the bound control. Then assign the DataItem of the Repeater to the ListItem in the ItemTemplate.

1
Dennis Traub On

I've only experimented with WebformsMVP a bit a couple of months ago, but as far as I can remember something along the lines of

foreach (var service in Model.Services)

should work. I think you don't need the Eval function here.