【问题标题】:Displaying Navigation Properties on a GridView using EntityDataSource?使用 EntityDataSource 在 GridView 上显示导航属性?
【发布时间】:2011-04-04 14:41:13
【问题描述】:

我有一个 EntityDataSource 我已映射到实体 Resident,其中包括两个导航属性(Building1、Room1)。我已将 GridView 设置为使用此 EntityDataSource 并将 EntityDataSource Include 属性设置为 Building1、Room1,以便它包含这些导航属性并将这些列添加到 GridView。当我运行应用程序而不是显示关联的导航属性时,它会显示:webHousingAdmin.Building 我怎样才能让它显示实际值? GridView 的代码如下所示:

        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lbl1" runat="server" Text='<%# Bind("Building1") %>' />
            </ItemTemplate>
        </asp:TemplateField>

我已经通过使用以下代码来显示实际值:

            <asp:TemplateField HeaderText="Building">
            <ItemTemplate>
                <asp:Label ID="lblBuilding" Text='<%# Bind("Building1.building_name") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>

但是有没有更简单的方法来做到这一点?这仅显示文本,不允许我对其进行编辑...如果我可以将其作为理想的边界字段。

【问题讨论】:

  • 您似乎试图将属性 Building1、Room1 等直接绑定到 GridView 中的列。因为这些是类而不是简单的标量属性 .ToString() 被调用,它只返回类名(如果你没有覆盖它)。但这只是一种猜测,您需要展示代码才能获得有根据的答案。

标签: asp.net entity-framework entity-framework-4 entitydatasource


【解决方案1】:

要在标签中获得有意义的内容,您可以将 Building 类的标量属性绑定到 Label ...

<asp:Label ID="lbl1" runat="server" Text='<%# Bind("Building1.Name") %>' />

...或者你可以覆盖类的ToString() ...

public class Building
{
    public string Name { get; set; }
    public string AnotherText { get; set; }

    public override string ToString()
    {
        return string.Concat(Name, ", ", AnotherText); // or whatever you like
    }
}

如果您将属性绑定到作为类的网格,则绑定引擎将调用ToString() - 如果您不覆盖ToString,则仅返回完整的类名(命名空间点类名)。这解释了为什么您在示例中只看到 webHousingAdmin.Building

编辑

与这个问题并不真正相关:但是如果您尝试将导航属性与Bind(不仅是Eval)绑定以在数据源和网格之间进行双向通信,您可能会遇到问题。可能它不会工作。请参阅此相关问题和答案:

Columns of two related database tables in one ASP.NET GridView with EntityDataSource

【讨论】:

    【解决方案2】:

    这是几年前问的,但我发现寻找相同的答案。这对我有用。在 ASPX 中,这是遵循所述模型的完整 GridView:

    <asp:EntityDataSource ID="dsResidents" runat="server"
            ConnectionString="name=connString"
            DefaultContainerName="dbContext"
            EntitySetName="Residents"
            Include="Building"
            EnableUpdate="true" />
    
        <asp:GridView ID="ResidentGrid" runat="server"
            DataSourceID="dsResidents"
            OnRowUpdating="ResidentsGrid_RowUpdating"
            AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblID"
                            Text='<%# Eval("residentId") %>'
                            runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName"
                            Text='<%# Eval("residentName") %>'
                            runat="server" />
                    </ItemTemplate>
                <asp:TemplateField HeaderText="CountryCode">
                    <ItemTemplate>
                        <asp:Label ID="lblCountryCode"
                            Text='<%# Eval("Building.number") %>'
                            runat="server" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:EntityDataSource ID="dsBuildings" runat="server"
                            ConnectionString="name=connString"
                            DefaultContainerName="dbContext"
                            EntitySetName="Buildings" />
    
                        <asp:DropDownList ID="ddlBuilding"
                            DataSourceID="dsBuildings"
                            DataTextField="number"
                            DataValueField="id"
                            SelectedValue='<%# Eval("id") %>' <!-- I am not sure about this line -->
                            runat="server" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    

    请注意,如果您使用向导创建实体模型,默认情况下,ConnectionString 名称与 DefaultContainerName 相同。在支持类中添加用于更新网格行的事件处理程序(OnRowUpdating):

    protected void ResidentsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // This is for retrieving the entity object for the resident selected
        Label idLabel = (Label)ResidentsGrid.Rows[e.RowIndex].Cells[1].FindControl("lblID");
        int residentId = int.Parse(idLabel.Text);
    
        // And this is for the building selected in the dropdown
        DropDownList ddl = (DropDownList)ResidentsGrid.Rows[e.RowIndex].Cells[4].FindControl("ddlBuilding");
        // I would like to say that the cell index is the column position but as I know it isn't
    
        using (dbContext ctx = new dbContext())
        {
            Resident resident = ctx.Residents
                .Where(res => res.residentId == residentId).First();
    
            Building selectedBuilding = ctx.Buildings
                .Where(bld => bld.id == ddl.SelectedItem.Value).First();
    
            resident.Building = selectedBuilding;
    
            ctx.SaveChanges();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      相关资源
      最近更新 更多