MVC3中的SelectList

时间:2013-02-13 15:34:03

标签: asp.net-mvc-3

我有一个MVC3页面,必须dropdownList包含Sex(男性,女性)和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;
}

并且在获取Register的操作时我有这个代码:

 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个值,我该怎么办?

0 个答案:

没有答案