我有一个 MVC3 页面,它必须在下拉列表中包含 Sex(male,female) 和 Role(admin,Operator),在 RegisterViewModel 中我已经这样做了:
public List<SelectListItem> SelectedItemForSex { get; set; }
public List<SelectListItem> SelectedItemForRole { get; set; }
public string SearchText { get; set; }
public string SelectedValue { get; set; }
//public string SelectedValueForRole { get; set; }
public static RegisterViewModel Get()
{
var model = new RegisterViewModel { SelectedItemForSex = new List<SelectListItem>() };
model.SelectedItemForSex.Add(new SelectListItem() { Text = "Male", Value = "1" });
model.SelectedItemForSex.Add(new SelectListItem() { Text = "Femle", Value = "2" });
model.SelectedItemForRole.Add(new SelectListItem() {Text = "Administrator", Value = "1"});
model.SelectedItemForRole.Add(new SelectListItem() {Text = "Operator", Value = "2"});
return model;
}
在注册操作中,我有这个代码:
public ActionResult Create()
{
var model = RegisterViewModel.Get();
return View(model);
}
还有剃须刀:
<div>
@Html.LabelFor(register => register.Sex)
@Html.DropDownListFor(register => register.SelectedValue, new SelectList(Model.SelectedItemForSex, "Value", "Text"))
</div>
<div>
@Html.LabelFor(register => register.Role)
@Html.DropDownListFor(register => register.SelectedValue, new SelectList(Model.SelectedItemForRole, "Value", "Text"))
</div>
<div>
我知道我没有为管理员和操作员初始化 selectedListItem,我想通过 Get() 方法发送所有这 4 个值,我该怎么做?