【发布时间】:2015-11-24 08:39:33
【问题描述】:
我在View 中有一个下拉列表
@Html.DropDownList("GenderAllowed", (SelectList)ViewBag.LocGenederAllowedList, new { @class = "form-control" })
我通过ViewBag 发送下拉列表,并通过模型发送需要在下拉列表中选择的值。
但是下拉列表中的值没有被选中。
我的控制器
[HttpGet]
public ActionResult EditVendorLocation(int VendorLocationID)
{
VendorLocationHandler obj = new VendorLocationHandler();
LocationGrid objLoc = new LocationGrid();
FillGenederAllowed(objLoc);
objLoc = obj.GetVendorLocationForAdmin(VendorLocationID);
return View(objLoc);
}
Viewbag 的功能
public void FillGenederAllowed(LocationGrid V)
{
Dictionary<int, string> LocGenederAllowed = EnumHelper.GetGenderStates();
SelectList LocGenederAllowedList = new SelectList(LocGenederAllowed, "key", "value");
ViewBag.LocGenederAllowedList = LocGenederAllowedList;
}
【问题讨论】:
-
模型是一个值列表。您是否将它们中的任何一个设置为
Selected = true? -
只需将属性
GenderAllowed的值设置为与您的选项之一的值匹配,它将被选中。
标签: c# asp.net-mvc model-view-controller html-select