【问题标题】:parameters dictionary contains null entry for parameter 'departmentId'参数字典包含参数“departmentId”的空条目
【发布时间】:2020-02-08 01:57:58
【问题描述】:
public class EmployeeController : Controller
{
    // GET: Employee
    public ActionResult Index(int departmentid)
    {
        EmployeeContext employeeContext = new EmployeeContext();

        List<Employee> employees = employeeContext.Employees
                                                  .Where(emp => emp.DepartmentId == departmentid)
                                                  .ToList();

        return View(employees);
    }

    public ActionResult Details(int id)
    {       
           EmployeeContext employeeContext = new EmployeeContext();

           Employee employee = employeeContext.Employees
                                              .Single(x => x.EmployeeId == id);

           return View(employee);
    }
}

departmentid 是参数字典,其中包含不可为空类型的参数“departmentId”的空条目。

【问题讨论】:

  • 欢迎来到 SO!您需要澄清您的问题是什么以及解决该问题需要什么。这里没有足够的信息来帮助您解决问题。请编辑您的帖子,您将有更好的机会获得有用的答案。

标签: asp.net-mvc


【解决方案1】:

在从客户端调用特定端点时,您没有传递 departmentid

如果参数是可选的,则为该参数设置一个默认值,如下所示

public ActionResult Index(int departmentid=0)

或者让它可以为空

public ActionResult Index(int? departmentid)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2018-01-21
    • 1970-01-01
    • 2017-05-12
    • 2013-12-15
    • 1970-01-01
    相关资源
    最近更新 更多