我想在 DropDown 中将 VieBage 项目作为选择列表,这是我的 ViewModel 内容
public List<SelectListItem> SelectedItemForRole { get; set; }
public string SelectedValue { get; set; }
public static RegisterViewModel GetRole()
{
var model = new RegisterViewModel { SelectedItemForRole = new List<SelectListItem>() };
model.SelectedItemForRole.Add(new SelectListItem() { Text = "Administrator", Value = "1" });
model.SelectedItemForRole.Add(new SelectListItem() { Text = "Operator", Value = "2" });
return model;
}
这是我的动作内容:
ViewBag.RoleList = RegisterViewModel.GetRole();
我在 View 中所做的是这样的:
<div>
@Html.LabelFor(register => register.Role)
@Html.DropDownListFor(register => register.SelectedValue, new SelectList(ViewBag.RoleList, "Value", "Text"))
</div>
但它不起作用!