Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 4 DropdownListfor错误 [AllowAnonymous] 公众行动结果登记册() { ViewBag.roleList=DALUser.GetRoleList(); ViewBag.stateList=DALUser.GetStateList(); 返回视图(); } @LabelFor(m=>m.role) @DropDownListFor(m=>m.role,新选择列表(ViewBag.roleList,“选择角色”))_C#_Asp.net - Fatal编程技术网

C# MVC 4 DropdownListfor错误 [AllowAnonymous] 公众行动结果登记册() { ViewBag.roleList=DALUser.GetRoleList(); ViewBag.stateList=DALUser.GetStateList(); 返回视图(); } @LabelFor(m=>m.role) @DropDownListFor(m=>m.role,新选择列表(ViewBag.roleList,“选择角色”))

C# MVC 4 DropdownListfor错误 [AllowAnonymous] 公众行动结果登记册() { ViewBag.roleList=DALUser.GetRoleList(); ViewBag.stateList=DALUser.GetStateList(); 返回视图(); } @LabelFor(m=>m.role) @DropDownListFor(m=>m.role,新选择列表(ViewBag.roleList,“选择角色”)),c#,asp.net,C#,Asp.net,它会导致 “值不能为null。参数名称:items”。角色列表和状态列表 这些只是字符串列表 有人对此有想法吗 一般来说,当您给SelectList构造函数一个空值时,您会看到该错误。您需要在ViewBag.roleList=DALUser.GetRoleList()上设置断点并验证返回的不是null。请记住,IList对象的默认值是null好的,列表项正确地显示在下拉列表上。当我单击“提交”按钮时,它会导致一个错误。“当我单击“提交”按钮时,它会导致一个错误。”-您是否在HttpPost操作中

它会导致

“值不能为null。参数名称:items”。角色列表和状态列表 这些只是字符串列表


有人对此有想法吗

一般来说,当您给
SelectList
构造函数一个空值时,您会看到该错误。您需要在
ViewBag.roleList=DALUser.GetRoleList()上设置断点
并验证返回的不是
null
。请记住,
IList
对象的默认值是
null

好的,列表项正确地显示在下拉列表上。当我单击“提交”按钮时,它会导致一个错误。“当我单击“提交”按钮时,它会导致一个错误。”-您是否在
HttpPost
操作中重新填充视图包?@user1713153正如CodeCaster所暗示的,集合不会在HTTP请求中持久化,除非您“隐藏”在表单的某个位置(这就是
ViewState
机制的工作原理)。请记住,MVC不是WebForms-您需要重新创建任何未绑定到输入元素的数据。
DropDownList
绑定到“selected value”属性,而不是集合。请尝试以下操作:新建SelectList((ListViewBag.roleList,“选择角色”))
    [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>