我的下拉菜单可以显示,但始终为空。
我尝试使用ViewBag,DropDownList和List,但这些都不会为我填充下拉列表。
型号
public IEnumerable<SelectListItem> AccountTypeECheck { get; set; }
控制器
ViewBag.AccountTypeECheck = new List<SelectListItem>() {
new SelectListItem() {
Value = "creditCard",
Text = "creditCard"
}
};
查看
@Html.DropDownList(
"value",
(IEnumerable<SelectListItem>) ViewBag.AccountTypeECheck,
null,
new { @class ="form-control" }
)
答案 0 :(得分:0)
尝试一下:
控制器:
var selectListItems = new List<SelectListItem>() { new SelectListItem() { Value = "creditCard", Text = "creditCard" } };
ViewBag.AccountTypeECheck = new SelectList(selectListItems, "Value", "Text");
查看:
@Html.DropDownList("AccountTypeECheck", null, new { @class ="form-control" })
将ViewBag
的属性名称(在您的示例中为AccountTypeECheck
)用作name
的{{1}}参数。
答案 1 :(得分:0)
您缺少模型成员,请尝试以下操作:
型号:
public IEnumerable<SelectListItem> AccountTypeECheck { get; set; }
public string AccountType {get;set;}
操作方法:
var model = new Model();
model.AccountTypeECheck = new List<SelectListItem>() {
new SelectListItem() {
Value = "creditCard",
Text = "creditCard"
}
};
return View(model);
查看:
@model Model
@Html.DropDownListFor(x => x.AccountType, Model.AccountTypeECheck)