MVC 4 DropdownListfor error

时间:2013-05-15 07:12:02

标签: c# asp.net

    [AllowAnonymous]
    public ActionResult Register()
    {
      ViewBag.roleList = DALUser.GetRoleList();
      ViewBag.stateList = DALUser.GetStateList();
      return View();
    }

<li>
    @Html.LabelFor(m => m.role)
    @Html.DropDownListFor(m => m.role, new SelectList(ViewBag.roleList, "Select Role"))
</li> 

导致

  

“值不能为null。参数名称:items”。 roleList和stateList   只是字符串列表

有人对此有所了解吗?

1 个答案:

答案 0 :(得分:1)

一般来说,当您为SelectList构造函数赋予空值时,您将看到该错误。您需要在ViewBag.roleList = DALUser.GetRoleList();上放置一个断点,并验证是否返回了null以外的其他内容。请记住,IList对象的默认值为null