使用iEnumerable填充下拉列表

时间:2012-11-19 20:01:28

标签: asp.net vb.net entity-framework linq ienumerable

如何在转发器中为iEnumerable参与者填充下拉列表?

    Dim ac = pl.pages _
        .Where(Function(p) p.urlName = "2012Basketball" And p.organization.urlName = "CopleyHS") _
        .SelectMany(Function(p) p.actions) _
        .GroupBy(Function(s) s.eventId) _
        .[Select](Function(g) New With { _
          Key .EventId = g.Key, _
          Key .EventName = g.FirstOrDefault().event.name, _
          Key .Actions = g.[Select](Function(s) New With { _
            Key .ActionId = s.id, _
            Key .ActionName = s.name, _
            Key .Participants = s.event.participants.[Select](Function(k) New With { _
                Key .ParticipantName = k.name, _
                Key .ParticipantId = k.id _
            }) _
          }) _
       })

    rep_Events.DataSource = ac
    rep_Events.DataBind()

的.aspx:

<asp:Repeater ID="rep_Events" runat="server">
    <ItemTemplate>
        <b><%#Eval("EventName")%></b>
        <asp:Repeater ID="rep_Actions" runat="server" Datasource='<%#Eval("Actions") %>'>
            <ItemTemplate>
                <blockquote>
                    <%#Eval("ActionName")%> <br />
                    <asp:DropDownList ID="dd_participants" runat="server" DataSource='<%# Eval("Participants") %>' DataTextField='<%# Eval("ParticipantName") %>' DataValueField='<%#Eval("ParticipantId")%>'>
                    </asp:DropDownList>
                </blockquote>
            </ItemTemplate>
         </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

错误发生在asp:DrowDownList,声明VB $ AnonymousType_1不包含名为ParticipantName的属性。

DataBinding: 'VB$AnonymousType_1`3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.IEnumerable`1[[VB$AnonymousType_2`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], App_Web_2earclkn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'ParticipantName'.

我尝试使用.ToList和.AsEnumerable转换参与者,但仍然遇到错误。

任何帮助都将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

当然这属于第二个列表,Actions没有ParticipantName属性。该物业属于第三个清单的项目。所以您可以按如下方式更改代码:

 <asp:DropDownList ID="dd_participants" runat="server" DataSource='<%# Eval("Participants") %>' DataTextField="ParticipantName" DataValueField="ParticipantId">
                </asp:DropDownList>

希望有所帮助。