无法在 MVC 中绑定 DropDownList
not able to bind a DropDownList in MVC
我是 MVC
的新手。我正在尝试绑定下拉列表但遇到问题。
DataLayer代码如下:
public List<DataLayer.Customer> GetCustomers()
{
return obj.Customers.ToList();
}
控制器代码:
[Authorize]
public ActionResult CreateOrder()
{
ViewBag.Message = "Crearte Order";
ViewBag.Customers = manageOrder.GetCustomers();
return View();
}
查看代码:
@Html.DropDownList("SelectedMovieType", (IEnumerable<SelectListItem>) ViewBag.Customers)
尝试绑定 DropDownList
时出现以下错误
允许转换类型为 'System.Collections.Generic.List1[DataLayer.Customer]' to type 'System.Collections.Generic.IEnumerable
1[System.Web.Mvc.SelectListItem]' 的对象。
让我知道如何解决这个问题。
ViewBag.Customers
应该是类型 List<SelectListItem>
.
控制器代码:
ViewBag.Customers = manageOrder.GetCustomers().Select(c => new SelectListItem { Text = c.TextProperty, Value = c.ValueProperty }).ToList();
查看代码:
@Html.DropDownList("Customers")
我是 MVC
的新手。我正在尝试绑定下拉列表但遇到问题。
DataLayer代码如下:
public List<DataLayer.Customer> GetCustomers()
{
return obj.Customers.ToList();
}
控制器代码:
[Authorize]
public ActionResult CreateOrder()
{
ViewBag.Message = "Crearte Order";
ViewBag.Customers = manageOrder.GetCustomers();
return View();
}
查看代码:
@Html.DropDownList("SelectedMovieType", (IEnumerable<SelectListItem>) ViewBag.Customers)
尝试绑定 DropDownList
允许转换类型为 'System.Collections.Generic.List1[DataLayer.Customer]' to type 'System.Collections.Generic.IEnumerable
1[System.Web.Mvc.SelectListItem]' 的对象。
让我知道如何解决这个问题。
ViewBag.Customers
应该是类型 List<SelectListItem>
.
控制器代码:
ViewBag.Customers = manageOrder.GetCustomers().Select(c => new SelectListItem { Text = c.TextProperty, Value = c.ValueProperty }).ToList();
查看代码:
@Html.DropDownList("Customers")