asp.nethtmlwebformsmvp

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


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!


Solution

  • 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.