Populating DropDownList with Entity Framework 4

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

I am currently using this code:

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

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

+3
source share
4 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();                
         }
+7
source
using (dbEntities db = new dbEntities())
{
    ddlNewEmployee.DataSource = (from emp in db.CC_EMPLOYEE
                                    select new { emp.EmployeeID, emp.EmployeeCode }).ToList();

    ddlNewEmployee.DataTextField = "EmployeeCode";
    ddlNewEmployee.DataValueField = "EmployeeID";
    ddlNewEmployee.DataBind();
}
0
source

This works great:

private COFFEESHOPEntities1 CoffeeContext = new COFFEESHOPEntities1();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //getData();
        cbxCategory.DataSource = CoffeeContext.tblProductTypes.ToList();
        cbxCategory.DataTextField = "Description";
        cbxCategory.DataValueField = "ProductType";
        cbxCategory.DataBind();
    }
}
0
source
private COFFEESHOPEntities1 CoffeeContext = new COFFEESHOPEntities1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //getData();
            cbxCategory.DataSource = CoffeeContext.tblProductTypes.ToList();
            cbxCategory.DataTextField = "Description";
            cbxCategory.DataValueField = "ProductType";
            cbxCategory.DataBind();

        }
    }
0
source

Source: https://habr.com/ru/post/1785881/


All Articles