Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a DropDownList using Entity Framework 4

I need a very simple example of code to populate a DropDownList using Entity Framework 4.

At the moment I use this code:

        using (TestHierarchyEntities context = new TestHierarchyEntities())
        {
            uxSelectNodeDestinationDisplayer.DataSource = context.CmsCategories.ToList();
            uxSelectNodeDestinationDisplayer.DataBind();
        }

But it does not work properly... Any idea? Thanks

like image 911
GibboK Avatar asked Jan 20 '23 17:01

GibboK


1 Answers

Something like this should work :

using (TestHierarchyEntities context = new TestHierarchyEntities())
        {
                var category = (from c in context.context
                                select new { c.ID, c.Desc }).ToList();

                DropDownList1.DataValueField = "MID";
                DropDownList1.DataTextField = "MDesc";
                DropDownList1.DataSource = category;
                DropDownList1.DataBind();                
         }
like image 188
Dewald Henning Avatar answered Jan 28 '23 05:01

Dewald Henning