1

我有兩個實體類:OrderOrderItemOrder包含類型數據綁定異常與實體導航屬性

System.Data.Objects.DataClasses.EntityCollection<OrderItem> 

在aspx頁的導航屬性OrderItemSet綁定到此EntityDataSource一個FormView:

<asp:EntityDataSource ID="EntityDataSourceOrder" runat="server" 
    ConnectionString="name=EntitiesContext" 
    DefaultContainerName="EntitiesContext" 
    EntitySetName="Order" 
    Include="OrderItemSet" 

    // stuff to define a query 

</asp:EntityDataSource> 

而在FormView綁定到數據源,這的FormView的ItemTemplate中包含一個ListView我嘗試綁定到OrderItemSet。它看起來是這樣的:

<asp:FormView ID="FormViewOrder" runat="server" DataKeyNames="OrderID" 
       DataSourceID="EntityDataSourceOrder" AllowPaging="True" > 
    <ItemTemplate> 
     ... 

     <asp:ListView ID="ListViewOrderItems" runat="server" 
         DataSource='<%# Eval("OrderItemSet")%>' > 
      ... 
     </asp:ListView> 
    </ItemTemplate> 
</asp:FormView> 

當我運行該應用程序我得到一個異常指着標記線DataSource='<%# Eval("OrderItemSet")%>',告訴我:

數據綁定:System.Web.UI.WebControls.EntityDataSourceWrapper不包含名稱爲'OrderItemSet'的屬性

這裏有什麼問題?

(我已經做了這不是列表,但單一的對象引用其他導航性能是相同的,那工作。)

感謝您的幫助!

回答

1

在我看來,你正在試圖從數據源中評估一個集合,而沒有首先綁定到那個數據源。

爲什麼不嘗試直接綁定到數據源?例如。

<asp:ListView ID="ListViewOrderItems" runat="server" 
      DataSourceID="EntityDataSourceOrder" 
... 
</asp:ListView> 
+0

你好西安,謝謝你的回覆!其實我的數據源綁定到一個FormView。而我所說的ListView是這個FormView的ItemTemplate的一部分。所以我不想將ListView綁定到訂單的「完整」數據源,而只是綁定到訂單的特定導航屬性(OrderItems列表)。我試圖在上面的問題中澄清這一點。 – Slauma 2010-03-11 15:19:25

+0

也許FormView的數據綁定發生得太晚了。您可以嘗試連接到FormView的OnDataBound事件,然後以編程方式設置ListViewOrderItems的DataSource。 – codemonkeh 2010-03-14 21:49:53

+0

謝謝西安!這就是我現在正在做的事情,它的工作原理。也許你是對的,數據綁定已經太遲了,但是這個異常的錯誤信息是相當混亂的。我現在要關閉這個問題。 – Slauma 2010-03-18 20:31:52