@Html.DropDownList("UserTypeId",new SelectList(ViewBag.Datalist, "UserTypeId", "UserTypeName"), new { style = "width:155px;margin-right:5px;" })我想将dropdownlist中的值添加到数据库中,为什么dropdownlist在点击添加时会提示:值不能为 null。
参数名: items
,而且有时是可以添加的,有时就会提示错误

解决方案 »

  1.   

    ViewBag.Datalist 会不会是null值?
      

  2.   

    不是空值,ViewBag.Datalist一直都有值,他是从数据库中读取出来的,显示在dropdownlist中,然后再添加到数据库另一个表中
      

  3.   

    非常有可能是 ViewBag.Datalist  为null
      

  4.   

    我非常确定ViewBag.Datalist他有值,要不怎么能显示在dropdownlist中呢
      

  5.   

      public ActionResult Create()
            {
                UserInfoModels userInfo1 = new UserInfoModels();
                UserInfoBLL userInfo = new UserInfoBLL();
                List<UserTypeModels> list = userInfo.GetUserTypeInfo();
                ViewBag.Datalist = list;
                return View(userInfo1);
            } 
    这是我Action中的代码,这有值
      

  6.   

    我从数据库中读取数据显示在dropdownlist中,然后我在dropdownlist下拉列表中选择一项,点击添加按钮,添加到数据库中,其中再点击添加按钮时出现值不能为空的错误
      

  7.   

    点击添加按钮时 执行的action 是怎样的
      

  8.   

    我的起始页就是Create页:
      public ActionResult Create()
            {
                UserInfoModels userInfo1 = new UserInfoModels();
                UserInfoBLL userInfo = new UserInfoBLL();
                List<UserTypeModels> list = userInfo.GetUserTypeInfo();
                ViewBag.Datalist = list;
                return View(userInfo1);
            } 
    查询数据并显示在dropdownlist中,
    点击添加:(下面没执行,就出现dropdownlist值不能为空的错误)
    [HttpPost]
            public ActionResult Create(UserInfoModels user)
            {
                try
                {
                    // TODO: Add insert logic here
                    UserInfoBLL userInfo = new UserInfoBLL();
                    userInfo.Update(user);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
      

  9.   

    看看是不是list中的某些项为null导致的重点看出错时  的选项
      

  10.   

    我在list那加断点,页面还是直接就显示,跟没加一样
      

  11.   

    那就应该和数据有关系 还是看返回的list