作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 View
中有一个下拉菜单
@Html.DropDownList("GenderAllowed", (SelectList)ViewBag.LocGenederAllowedList, new { @class = "form-control" })
我正在通过 ViewBag
发送下拉列表,并通过模型发送需要在下拉列表中选择的值。
但下拉列表中的值未被选中。
我的 Controller
[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;
}
最佳答案
您传递给 DropDownList 的 SelectListItems
具有属性 Selected
。在您的 ViewModel 中,将最初应选择的项目设置为 true。
关于c# - 如何在下拉列表mvc中设置选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888898/
我是一名优秀的程序员,十分优秀!