我该如何纠正? [英] How do I correct this?
问题描述
参数字典包含非可空类型'System.Int32'的参数'ID'的空条目,用于'KudvenkatDemo.Controllers.EmployeeController'中方法'System.Web.Mvc.ActionResult Details(Int32)'。可选参数必须是引用类型,可空类型,或者声明为可选参数。参数名称:参数。
我尝试过:
我的controller代码:
public ActionResult Details(int ID)
{
EmployeeContext employeeContext = new EmployeeContext() ;
员工员工= employeeContext.Employees.Single(emp => emp.EmpID == ID);
返回查看(员工);
}
我的查看代码:
@ {
ViewBag.Title =员工详细信息;
布局=〜/ Views / Shared / _Layout.cshtml;
}
员工详情
员工ID | @Model。 EmpID |
员工姓名 | @ Model.Name |
员工年龄 | @ Model.Age |
City | @ Model.City |
The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'KudvenkatDemo.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters.
What I have tried:
My controller Code:
public ActionResult Details(int ID)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee= employeeContext.Employees.Single(emp => emp.EmpID == ID);
return View(employee);
}
My View Code :
@{
ViewBag.Title = "Employee Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Employee Details
Employee ID | @Model.EmpID |
Employee Name | @Model.Name |
Employee Age | @Model.Age |
City | @Model.City |
推荐答案
你可能在没有身份证的情况下调用你的行动;例如网址是
/ MyController / Details
而不是
/ MyController / Details / 123
如果您希望ID是可选的,请更改您的行动
You're probably calling your action with no ID; eg the url is
/MyController/Details
rather than
/MyController/Details/123
If you want the ID to be optional then change your action
public ActionResult Details(int? ID)
并将您的代码更改为ID为空时处理
and change your code to handle when the ID is null
这篇关于我该如何纠正?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!