使用 EntityDataSource 控件时将导航属性绑定到 RadGrid

发布于 2024-08-21 13:45:18 字数 4885 浏览 10 评论 0原文

我是实体框架的新手,在尝试将导航属性(外键引用)绑定到下拉列表时遇到了问题。我有 Telerik RadGrid 控件,它使用 EntityDataSource 控件获取数据。以下是模型描述:

Applications: AppId, AppName, ServerId
Servers: ServerId, ServerName

Applicaitons.ServerId 是对 Servers.ServerId 的外键引用。 RadGrid 列出了应用程序并允许用户插入/更新/删除应用程序。我想在编辑模式下将服务器名称显示为下拉列表,但我无法这样做。 。这是我的 aspx 代码:

<telerik:RadGrid ID="gridApplications" runat="server" Skin="Sunset" 
    AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
    AllowPaging="True" AllowAutomaticUpdates="True" 
    AutoGenerateColumns="False" OnItemCreated="gridApplications_ItemCreated" 
    DataSourceID="applicationsEntityDataSource" Width="50%" 
    OnItemInserted="gridApplications_ItemInserted" 
    OnItemUpdated="gridApplications_ItemUpdated" 
    OnItemDeleted="gridApplications_ItemDeleted" GridLines="None">
    <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="AppId" DataSourceID="applicationsEntityDataSource">
        <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="Edit" ItemStyle-Width="10%">
            </telerik:GridEditCommandColumn>
            <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Are you sure you want to delete this application?" ConfirmTitle="Confirm Delete" ConfirmDialogType="Classic" ItemStyle-Width="10%" HeaderText="Delete">
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="AppId" UniqueName="AppId" Visible="false" HeaderText="Application Id" ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="AppName" UniqueName="AppName" HeaderText="Application Name" MaxLength="30" ItemStyle-Width="40%">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="ServerId" UniqueName="ServerId" HeaderText="Server Hosted" EditFormColumnIndex="1">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlServerHosted" runat="server" DataTextField="Servers.ServerName" DataValueField="ServerId" Width="40%">
                    </asp:DropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings ColumnNumber="2" CaptionDataField="AppId" InsertCaption="Insert New Application" EditFormType="AutoGenerated">
            <EditColumn InsertText="Insert record" EditText="Edit application id #:" EditFormColumnIndex="0" UpdateText="Application updated" UniqueName="InsertCommandColumn1" CancelText="Cancel insert" ButtonType="ImageButton"></EditColumn>
            <FormTableItemStyle Wrap="false" />
            <FormTableStyle GridLines="Horizontal" CellPadding="2" CellSpacing="0" Height="110px" Width="110px" />
            <FormTableAlternatingItemStyle Wrap="false" />
            <FormStyle Width="100%" BackColor="#EEF2EA" />
            <FormTableButtonRowStyle HorizontalAlign="Right" />
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>
<asp:EntityDataSource ID="applicationsEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Applications" EnableInsert="True" EnableUpdate="True" EntitySetName="Applications" 
    DefaultContainerName="AnalyticsEntities" Include="Servers">
</asp:EntityDataSource>

我尝试了另一种方法,用以下代码替换 GridTemplateColumn

<telerik:RadComboBox ID="RadComboBox1" DataSourceID="serversEntityDataSource" DataTextField="ServerName" DataValueField="ServerId" AppendDataBoundItems="true" runat="server" >
<Items>
    <telerik:RadComboBoxItem />
</Items>

并使用单独的 EntityDataSource 控件,如下所示:

    <asp:EntityDataSource ID="serversEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Servers" EnableInsert="True" EnableUpdate="True" EntitySetName="Servers" 
    DefaultContainerName="AnalyticsEntities">
</asp:EntityDataSource>

但是,我收到以下错误。

Application cannot be inserted. Reason: Entities in 'AnalyticsEntities.Applications' participate in the 'FK_Servers_Applications' relationship. 0 related 'Servers' were found. 1 'Servers' is expected. 

我的问题是,如何绑定导航属性并加载 DropDownList/RadComboBox 控件中的值?

I'm new to Entity Framework and I got stuck in an issue while trying to bind a navigation property (foreign key reference) to a dropdownlist. I have Telerik RadGrid control which gets the data using a EntityDataSource control. Here is the model description:

Applications: AppId, AppName, ServerId
Servers: ServerId, ServerName

The Applicaitons.ServerId is a foreign key reference to Servers.ServerId. The RadGrid lists the applications and allows the user to insert/update/delete an application. I want to show the server names as a dropdownlist in edit mode which I'm not able to. . Here is my aspx code:

<telerik:RadGrid ID="gridApplications" runat="server" Skin="Sunset" 
    AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
    AllowPaging="True" AllowAutomaticUpdates="True" 
    AutoGenerateColumns="False" OnItemCreated="gridApplications_ItemCreated" 
    DataSourceID="applicationsEntityDataSource" Width="50%" 
    OnItemInserted="gridApplications_ItemInserted" 
    OnItemUpdated="gridApplications_ItemUpdated" 
    OnItemDeleted="gridApplications_ItemDeleted" GridLines="None">
    <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="AppId" DataSourceID="applicationsEntityDataSource">
        <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="Edit" ItemStyle-Width="10%">
            </telerik:GridEditCommandColumn>
            <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Are you sure you want to delete this application?" ConfirmTitle="Confirm Delete" ConfirmDialogType="Classic" ItemStyle-Width="10%" HeaderText="Delete">
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="AppId" UniqueName="AppId" Visible="false" HeaderText="Application Id" ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="AppName" UniqueName="AppName" HeaderText="Application Name" MaxLength="30" ItemStyle-Width="40%">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="ServerId" UniqueName="ServerId" HeaderText="Server Hosted" EditFormColumnIndex="1">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlServerHosted" runat="server" DataTextField="Servers.ServerName" DataValueField="ServerId" Width="40%">
                    </asp:DropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings ColumnNumber="2" CaptionDataField="AppId" InsertCaption="Insert New Application" EditFormType="AutoGenerated">
            <EditColumn InsertText="Insert record" EditText="Edit application id #:" EditFormColumnIndex="0" UpdateText="Application updated" UniqueName="InsertCommandColumn1" CancelText="Cancel insert" ButtonType="ImageButton"></EditColumn>
            <FormTableItemStyle Wrap="false" />
            <FormTableStyle GridLines="Horizontal" CellPadding="2" CellSpacing="0" Height="110px" Width="110px" />
            <FormTableAlternatingItemStyle Wrap="false" />
            <FormStyle Width="100%" BackColor="#EEF2EA" />
            <FormTableButtonRowStyle HorizontalAlign="Right" />
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>
<asp:EntityDataSource ID="applicationsEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Applications" EnableInsert="True" EnableUpdate="True" EntitySetName="Applications" 
    DefaultContainerName="AnalyticsEntities" Include="Servers">
</asp:EntityDataSource>

I tried another approach where I replaced the GridTemplateColumn with the following code

<telerik:RadComboBox ID="RadComboBox1" DataSourceID="serversEntityDataSource" DataTextField="ServerName" DataValueField="ServerId" AppendDataBoundItems="true" runat="server" >
<Items>
    <telerik:RadComboBoxItem />
</Items>

and using a separate EntityDataSource control as follows:

    <asp:EntityDataSource ID="serversEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Servers" EnableInsert="True" EnableUpdate="True" EntitySetName="Servers" 
    DefaultContainerName="AnalyticsEntities">
</asp:EntityDataSource>

but, I get the following error.

Application cannot be inserted. Reason: Entities in 'AnalyticsEntities.Applications' participate in the 'FK_Servers_Applications' relationship. 0 related 'Servers' were found. 1 'Servers' is expected. 

My question is, how do you bind the navigation property and load the values in the DropDownList/RadComboBox control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人│生佛魔见 2024-08-28 13:45:18

我相信您将下拉列表绑定到第二个数据源而不是网格的数据源(就像您在第二个示例中所做的那样)。然后,使用 <%# BIND("ServerId") %> 在下拉列表/组合框中设置 SelectedValue 属性。模板内的语法。

I believe you bind your dropdownlist to that second datasource instead of the grid's datasource (as you did in the second example). Then you set the SelectedValue property on the dropdown/combobox using the <%# BIND("ServerId") %> syntax within the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文