Scaffold DynamicData dropdown in web application from EF source

133 Views Asked by At

I want to scaffold a basic insert form which has dropdowns for the foreign keys.

I cant seem to figure out how to do this. When creating the metadata all the MetaColumns are of type MetaColumns, and none of type MetaForeignKeyColumns - which means that it renders textboxes for all properties - and no dropdown lists.

UIHints seem ineffective. I am using entity framework code first. How do I go about making those textboxes into dropdowns? In fact, I think the MetaData property is not being created ( I am probably missing something here ).

AddPermission.aspx (form only)

<asp:FormView runat="server" ID="AddPermissionForm"
ItemType="Common.Models.tag_permission"  
InsertMethod="AddPermissionsForm_InsertItem" DefaultMode="Insert"
RenderOuterTable="false" OnItemInserted="AddPermissionForm_ItemInserted">
<InsertItemTemplate>
    <fieldset>
        <ol>

            <asp:DynamicEntity runat="server" Mode="Insert" EnableViewState="true" ></asp:DynamicEntity>


          </ol>
        <asp:Button class="btn btn-primary" runat="server" Text="Insert" CommandName="Insert" />
        <asp:Button class="btn btn-default" runat="server" Text="Cancel" CausesValidation="false"  OnClick="CancelButton_Click" />
    </fieldset>
</InsertItemTemplate>

tag_permission.cs ( part of code first model )

public partial class tag_permission
{
    [ScaffoldColumn(false)]
    public short tp_tag_permission_id { get; set; }

    //foreign key one
    public string tp_security_group_id { get; set; }

    //foreign key two
    public short tp_tag_id { get; set; }

    [Display(Name = "View")]
    public Nullable<bool> tp_vis { get; set; }

}

I might have to use a DynamicDataManager or something, but I'm not sure where to find how or if it should be used on this page alongside the formview.

I've also attached screenshots comparing the rendering of Default_Insert.aspx.cs in my web app, and a basic DynamicData website - almost out of the box from template. In the below pictures, the MetaTable Table property is being inspected in DynamicData/Default_Insert.ascx.cs.

The later image has an incomplete MetaTable property. It has null values for the DataContextType and DataContextPropertyName, and ForeignKeyColumnNames. I'd really like to set the MetaTable on the FormView properly

For reference: The code from the images below is

public partial class Default_InsertEntityTemplate : System.Web.DynamicData.EntityTemplateUserControl {
    private MetaColumn currentColumn;

    protected override void OnLoad(EventArgs e) {
        foreach (MetaColumn column in Table.GetScaffoldColumns(Mode, ContainerType)) {
            currentColumn = column;
            Control item = new DefaultEntityTemplate._NamingContainer();
            EntityTemplate1.ItemTemplate.InstantiateIn(item);
            EntityTemplate1.Controls.Add(item);
        }
    }
...

DynamicData Web Site The dynamic data website has many MetaTable properties pouplated

My Web Application My web application has an incomplete MetaTable

1

There are 1 best solutions below

0
monkeyhouse On

Theres a few parts to this one:

On startup, I registered the dynamic data provider in global.asax

Global.asax.cs / other

App.DefaultModel.RegisterContext(
             new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(() => new MyDbContext()),
             new ContextConfiguration { ScaffoldAllTables = true });

App.cs ( a static class I use to store permanent references )

public static class App

  ...

   private static MetaModel s_defaultModel = new MetaModel();

   public static MetaModel DefaultModel
      {get{ return s_defaultModel; }}
}

then on the page where I want to get the meta data I can do this

ModelMetaTable meta = App.DefaultModel.GetTable( nameof(db.MyAwsomeName) );

and then set the metadata

MyAwesomeForm.SetMetaTable(table);

and then the form will render will all foreign keys / navigation properties as they would in a dynamic data website.

All the properties of the ModelMetadata are then set and populated as you would expect - eg with the DataContext and ForeignKeyName are no longer null