Asp.net mvc 4 从mvc4中的表加载带有数据的下拉列表
我有3个模型和相应的表(InquiryModel、EmployeeModel、RegionModel)。 我想在查询和员工的“创建”视图中填充一个下拉列表。 该下拉列表应填充RegionModel表中“Region_Name”字段中的数据 我该怎么做 我有区域模型:Asp.net mvc 4 从mvc4中的表加载带有数据的下拉列表,asp.net-mvc-4,Asp.net Mvc 4,我有3个模型和相应的表(InquiryModel、EmployeeModel、RegionModel)。 我想在查询和员工的“创建”视图中填充一个下拉列表。 该下拉列表应填充RegionModel表中“Region_Name”字段中的数据 我该怎么做 我有区域模型: namespace MvcConQuery.Models { [Table("Region_Details")] public class RegionModel {
namespace MvcConQuery.Models
{
[Table("Region_Details")]
public class RegionModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int32 Region_id { get; set; }
public string Region { get; set; }
}
}
还有EmployeeModel和InquiryModel,我在这些模型的控制器中给出了以下代码:
public ActionResult Create()
{
IEnumerable<SelectListItem> abc = new SelectList(db.Regions, "Region_id", "Region");
ViewBag.regions = abc;
return View();
}
但我得到了一个错误:
没有具有键“regions”的“IEnumerable”类型的ViewData项。
请帮助我编写控制器和创建视图中的Create()函数的代码。像这样尝试
DATAEntities _context = new DATAEntities ();
var List = (from r in _context.Registrations select r).ToList();
ViewBag.CustomerName = new SelectList(List, "Region_id", "Region");
查看
@Html.DropDownList("regions", (SelectList)ViewBag.CustomerName, "--Select--")
如果您有任何问题,请告诉我。您尝试过什么吗?是的,我在controller:public ActionResult Create(){IEnumerable abc=new SelectList(db.Regions,“Region_id”,“Region”);ViewBag.Regions=abc;return View();}我在Create视图中给出了这段代码:@Html.DropDownList(“regions”,“select”)发布了一些有意义的代码,我可以帮助您
@Html.DropDownList("regions", (SelectList)ViewBag.CustomerName, "--Select--")