Value cannot be null. Parameter name: items (in Dropdown List) ASP.NET MVC5

后端 未结 5 457
终归单人心
终归单人心 2020-12-02 00:18

I have problem in my code. I\'m using the registration form which comes with MVC5 , I added a field \"Role\" as a Dropdownlist to assign a role to the user while creating a

5条回答
  •  时光取名叫无心
    2020-12-02 01:05

    Make sure you add the list to the model before returning the view, or you will get this error. Example:

    cshtml:

    @model DelegatePortal.ViewModels.ImpersonateVendorViewModel
    
    
    @using (Html.BeginForm("ImpersonateVendor", "Admin", FormMethod.Post))
    {
        @Html.DropDownListFor(model => model.Id, new SelectList(Model.Vendors, "Id", "Name"), "Choose a vendor", new { @class = "form-control form-control-sm " })
    }
    

    controller:

    // GET: /Admin/ImpersonateVendor
        public ActionResult ImpersonateVendor()
        {
            ImpersonateVendorViewModel model = new ImpersonateVendorViewModel();
            var vendors = (from c in db.Vendors
                            select c).ToList();
            model.Vendors = vendors; --> add list here
            return View(model);
        }
    

提交回复
热议问题