Search code examples
asp.netdynamic-dataasp.net-dynamic-data

Foreign key table field not displaying within a standalone dynamic data enabled list view


I've gone through hundreds of Google result pages, trying to find an answer for this over the past couple of days.

Using a standalone dynamic data page to display a table, using an EntityDataSource, with one of the fields being a foreign key to a sub-table. I want it to display the value from the sub-table. I've been playing with a simplified case using the NorthWinds database (see code below). If I assign the DynamicControl a DataField="Supplier", it displays the sub-table/class name ("DAL.Supplier") instead. If I try assigning DataField="Supplier.CompanyName", it errors with "The table 'Product' does not have a column named 'Supplier.CompanyName'". Since I want to take advantage of dynamic data's editing features, using a templated field with <%# Eval("Supplier.CompanyName") %> is out.

Is this just not possible with a stand-alone dynamic data page? It clearly works fine within a fully scaffolded system. Or am I (hopefully) just missing something? Thank you.

Test.aspx

<%@ Page Title="" Language="VB" MasterPageFile="~/Master/Site.master" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" Runat="Server">

<asp:ListView ID="ListView1" runat="server" DataSourceID="theDataSource" DataKeyNames="ProductID">
    <ItemTemplate>
        <tr>
            <td>
                <asp:DynamicControl runat="server" DataField="ProductID" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="ProductName" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="Supplier" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="UnitPrice" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="Discontinued" Mode="Edit" />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table id="itemPlaceholderContainer" runat="server" border="0">
                        <tr runat="server">
                            <th runat="server">
                                ProductID
                            </th>
                            <th runat="server">
                                ProductName
                            </th>
                            <th runat="server">
                                Supplier
                            </th>
                            <th runat="server">
                                UnitPrice
                            </th>
                            <th runat="server">
                                Discontinued
                            </th>
                        </tr>
                        <tr id="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server">
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:ListView>

<asp:EntityDataSource ID="theDataSource" runat="server" 
        ConnectionString="name=NorthwindEntities" 
        DefaultContainerName="NorthwindEntities" EnableDelete="True" 
        EnableFlattening="False" EnableInsert="True" EnableUpdate="True" 
        Include="Supplier"
        EntitySetName="Products">
    </asp:EntityDataSource>
</asp:Content>

Test.aspx.vb

Imports System.Web.DynamicData
Imports DAL

Partial Class Test
Inherits System.Web.UI.Page

Protected table As MetaTable

Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    Listview1.EnableDynamicData(GetType(Product))
    table = theDataSource.GetTable()
    Title = table.DisplayName   
End Sub
End Class

Solution

  • Problem solved. When all of this had started, I had originally gotten the error message:

    Could not determine a MetaTable. A MetaTable could not be determined for the data source 'EntityDataSource1' and one could not be inferred from the request URL. Make sure that the table is mapped to the dats source, or that the data source is configured with a valid context type and table name, or that the request is part of a registered DynamicDataRoute

    Trying to track that down led me to getting rid of the asp:DynamicDataManager markup, and replacing it in the code-behind with:

    Listview1.EnableDynamicData(GetType(Product))
    table = theDataSource.GetTable()
    

    That turns out to have led me down the proverbial rabbit hole... While it got rid of the MetaTable error, it inadvertently caused the problem I wrote about above. Many thanks to: http://daviworld.net/?tag=/DynamicDataManager who points out that this error message is actually caused by:

    This is due to an issue with the Designer code generation. When you select a DataSource for the GridView control, it does not add the ContextTypeName Attribute to the EntityDataSourceControl.

    Once I removed the lines of code above, and added back in the asp:DynamicDataManager to the markup, and then added a ContextTypeName="DAL.NorthwindEntities" to the EntitiyDataSource, everything now works as one would wish it to.