How do I make a ManyToMany_Insert template for EF

2019-08-28 05:45发布

I'm extremely new to Dynamic Data, but I'll fire this off anyway.

I have an EF model set up, with a PJT table linking Shows and Genres. The EDMX correctly displays this as a * - * relationship between Shows and Genres, with navigation properties. Using Dynamic Data, I can create a Show item, but then only once it is created, can I edit it to link up the Genres (which are nicely displayed as check boxes).

I've read this http://blogs.msdn.com/b/davidebb/archive/2008/10/25/a-many-to-many-field-template-for-dynamic-data.aspx article which everyone refers to, but I can't see how to use the ManyToMany_Edit.ascx Field template on the Show insert page. Interestingly, in the ManyToMany_Edit.ascx.cs file that I have, there is a comment that says

// This field template is used both for Editing and Inserting

But I can't see it being used for inserting!

Any pointers would be greatly appreciated!

1条回答
不美不萌又怎样
2楼-- · 2019-08-28 05:58

For example, create such derived classes, change type of DefaultModel in Global.asax

public class MyMetaModel:MetaModel
{
     public MyMetaModel() : base()
     {
         this.FieldTemplateFactory = new MyFieldTemplateFactory();
     }

    protected override MetaTable CreateTable(System.Web.DynamicData.ModelProviders.TableProvider provider)
    {
        return new MyMetaTable(this, provider);
    }
}

public class MyMetaTable : MetaTable
{
    public MyMetaTable(MetaModel metaModel, TableProvider tableProvider)
        :base(metaModel, tableProvider)
    {
    }

    public override IEnumerable<MetaColumn> GetScaffoldColumns(System.Web.UI.WebControls.DataBoundControlMode mode, ContainerType containerType)
    {
        if (this.Name != "entity1set") return base.GetScaffoldColumns(mode, containerType);

        List<MetaColumn> result = new List<MetaColumn>();

        result.Add(this.GetColumn("Name"));
        result.Add(this.GetColumn("entity2"));  // this can be many to many column

        return result;
    }
}

best regards corporal_lael

查看更多
登录 后发表回答