简体   繁体   English

填充下拉列表返回 null 指针异常

[英]Populating dropdown list returning null pointer exception

A small project consisting of 2 classes Appointments and Department , I need to be able to select the department name from a dropdown box.一个由 2 个类AppointmentsDepartment组成的小项目,我需要能够从下拉框中 select 部门名称。

I have tried <select> and asp-items , both are returning nullpointerexcpetion or something similar like ArgumentNullException: Value cannot be null. (Parameter 'items')我试过<select>asp-items ,两者都返回nullpointerexcpetion或类似的东西ArgumentNullException: Value cannot be null. (Parameter 'items') ArgumentNullException: Value cannot be null. (Parameter 'items')

Appointment Class

    public class Appointment
    {
        [Key]
        public virtual int Id { get; set; }
 
        [Display(Name = "Department")]

        public virtual int Dep_Id { get; set; }

        public Department Departments{ get; set; }

    }

Department class
    public class Department
    {
        [Key]
        public virtual int ID { get; set; }
        [Required]
        public virtual string Name { get; set; }
    }

Two controllers using Entity Framework (Web API and MVC) were scaffolded based on these classes and localdb Department table was filled with some values.基于这些类搭建了两个使用实体框架的控制器(Web API 和 MVC),并用一些值填充了 localdb Department表。 In the MVC controller generated from Appointment , I created a method在从Appointment生成的 MVC controller 中,我创建了一个方法

        public void PopulateDepartmentsDropDownList()
        {
            var departmentsQuery = from d in _context.Departments
                                   orderby d.Name
                                   select d;
            ViewBag.DepartmentID = new SelectList(departmentsQuery.AsNoTracking(), "ID", "Name");
        }

And finally in the Create.cshtml also generated from Appointment , these attempts do not work.最后在也从Appointment生成的Create.cshtml中,这些尝试不起作用。

<select asp-for="Dep_Id" class="control-label" asp-items=ViewBag.DepartmentID></select>

generates an empty dropdown and生成一个空的下拉列表和

@Html.DropDownList("dep", new SelectList(ViewBag.DepartmentID, "ID", "Name")) crashes the page. @Html.DropDownList("dep", new SelectList(ViewBag.DepartmentID, "ID", "Name"))使页面崩溃。

I am not sticking to any method, any solution that works based on any controller is fine.我不坚持任何方法,任何基于任何 controller 的解决方案都可以。

I think you lack typecasting like in this question.我认为你缺乏像这个问题那样的类型转换。 I can not test it now, but i think this is the solution.我现在无法测试它,但我认为这是解决方案。 @Html.DropDownListFor("dep", ViewBag.DepartmentID as SelectList)

I think you are missing to convert variable to List我认为您缺少将变量转换为 List

In the controller:在 controller 中:

var departmentsQuery = from d in _context.Departments
                                   orderby d.Name
                                   select d;
    List<Department> department = departmentsQuery.ToList();
    
    ViewBag.DepartmentID = new SelectList(department, "ID", "Name");

In the View:在视图中:

@Html.DropDownList("DepartmentID", (IEnumerable<SelectListItem>)ViewBag.DepartmentID, null, new { @class ="form-control" })

or you can replace the "null" with whatever you want display as default selector, ie "Select Department".或者您可以将“null”替换为您希望显示为默认选择器的任何内容,即“选择部门”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM