C# 值不能为null。参数名称:项目MVC5(下拉列表)

C# 值不能为null。参数名称:项目MVC5(下拉列表),c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我的模态 public class Employee { [Key] public int ID { get; set; } [MaxLength(10,ErrorMessage ="Max Lenght 20")] public string Name { get; set; } [MaxLength(20,ErrorMessage ="Max Lenght 20")] public string Address { get; set; }

我的模态

public class Employee
{
    [Key]
    public int ID { get; set; }

    [MaxLength(10,ErrorMessage ="Max Lenght 20")]
    public string Name { get; set; }
    [MaxLength(20,ErrorMessage ="Max Lenght 20")]
    public string Address { get; set; }
    public decimal Salary { get; set; }
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    [Display(Name ="Date of Joining")]
    public DateTime Dateofjoin { get; set; }
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    public List<CityModal> City { get; set; }
    [Display(Name="City")]
    public int CityId { get; set; }
    public string CityName { get; set; }


}
public class CityModal {
    public string CityId { get; set; }
    public string CityName { get; set; }
}

在这里,我检查了控制器查询是否有结果,并且包含CityId,但视图显示“值不能为null。参数名称:items”。我认为create方法遇到了一些我没有遇到的问题。需要帮助。

ViewBag.city
是在您的
Create
操作的GET版本中设置的,但我想您可能忽略了在POST版本中再次设置它。但是,您已经发布了相应的代码。此外,它也不会在
编辑
操作中设置。这可能与当前的问题无关,但很可能很快就会成为问题本身


通常,例外情况是告诉您
参数(即
选择列表
构造函数的第一个参数)为空。如果在您的操作中未设置
ViewBag.city
,则会出现这种情况。由于它在GET版本中,我只能假设当再次返回视图以允许用户进行必要的编辑时,您在验证错误时收到此异常。

ViewBag.city已填充,但下拉列表中没有值?这意味着
ViewBag.city
的值为
null
<div class="form-group">
                @Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.CityId, new SelectList(ViewBag.city, "CityId", "CityName"), "Select")
                    @Html.ValidationMessageFor(model => model.CityId, "Please select the City", new { @class = "error" })
                </div>
            </div>
[HttpGet]
    public ActionResult Create()
    {
        using (RegMVCEntities obj = new RegMVCEntities())
        {
            ViewBag.city = obj.tblCities.Select(e => new CityModal
            {

                CityName = e.City,
                CityId = e.ID                    
            }).ToList();
        }
            return View();
    }
 [HttpGet]
    public ActionResult Edit(int id)
    {
        using (RegMVCEntities obj = new RegMVCEntities())
        {
            var query = (from reg in obj.tblRegistrations
                         join citi in obj.tblCities on reg.CityID equals citi.ID
                         where reg.ID == id
                         select new Employee
                         {
                             ID = reg.ID,
                             Name = reg.Name,
                             Address = reg.Address,
                             Salary = reg.Salary,
                             Dateofjoin = reg.DateOfJoining,
                             Email = reg.Email,
                             CityId = citi.ID,
                             CityName = citi.City
                         }).FirstOrDefault();

            /*var employee = obj.tblRegistrations.Where(m => m.ID == id).Select(e => new Employee {
                ID = e.ID,Name = e.Name, Address = e.Address, Dateofjoin = e.DateOfJoining, Salary = e.Salary, Email = e.Email
            }).FirstOrDefault();*/
            return View(query);
        }                
    }