开发者

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


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();                
         }


This works perfectly:

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();
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜