Entity framework 如何在下拉列表中显示CustomerId列表

Entity framework 如何在下拉列表中显示CustomerId列表,entity-framework,asp.net-mvc-4,html-select,Entity Framework,Asp.net Mvc 4,Html Select,我想在Customer表中显示的customerID的基础上创建Customer日记 在customerID字段中,我希望下拉列表包含数据库中存在的customerID列表 我该怎么做?有人能帮我吗 var _objAllCustomerIds = null; using (AdventureWorksEntities context = new AdventureWorksEntities()) { _objAllCustomerIds = context.MyCustomers.

我想在Customer表中显示的customerID的基础上创建Customer日记

在customerID字段中,我希望下拉列表包含数据库中存在的customerID列表

我该怎么做?有人能帮我吗

var _objAllCustomerIds = null; 
using (AdventureWorksEntities context = new AdventureWorksEntities())    
{

_objAllCustomerIds = context.MyCustomers.Select(customer =>     customer.Id).ToList();

}
//其中AdventureWorksEntities是包含所有实体的DBcontent //MyCustomers是customer表的实体表示


如果这在您的.aspx文件中

然后在.aspx.cs文件中(假设您使用的是代码隐藏),您将获得客户ID列表,然后将该列表绑定到此下拉列表

另一个问题的答案有用吗?

hi[HttpPost][ValidateAntiForgeryToken]公共操作结果创建(日记){if(!cd.Customers.Any(c=>c.CustomerID==Diary.CustomerID)){ModelState.AddModelError(“CustomerID”,“找不到客户”);}if(ModelState.IsValid){cd.Diaries.Add(日记);cd.SaveChanges()return RedirectToAction(“Index”,“Home”);}return PartialView(“_CreateDiary”)}这是我的代码。你能编辑这段代码吗?这是mvc的新手,不知道怎么做,你能编辑我的代码吗?呈现此页面的代码是显示部分(部分视图是_CreateDiary)视图的控制器。您可以通过在此控制器上放置调试器并跟随调试器到达该控制器来找到该控制器。我们将在控制器内部添加所需的代码。嘿,很抱歉,你能编辑我的控制器代码吗,我很困惑,请查看此链接。。仔细阅读,当您完成后,您将更好地了解该做什么…如果您在阅读后有任何问题,请询问我,我将解释您需要转到MVC站点,并通过一些教程了解使用mvc4的MCVm的基础知识,我只需要customerid列表,因为我的dairy from是基于CustomerID@kirandeep是的,当你这样做时会发生什么?