Parameter dictionary contains null entry for parameter 'idd' of non-nullable type 'System.Int32'
I am a newbie here to learn MVC. I hit this error, I couldn't find a solution.
My controller:
public class EmployeeDataController : Controller
{
public ActionResult Details(int idd)
{
EmployeeDataContext edc = new EmployeeDataContext();
EmployeeData employee= edc.employeedata.Single(emp => emp.id == idd);
return View(employee);
}
}
When executing the Details method from the browser (i.e. http: // localhost: 4166 / EmployeeData / details / 1 ), I get this error:
The parameter dictionary contains a null entry for the 'idd' parameter of type other than nullable 'System.Int32' for the 'System.Web.Mvc.ActionResult Details (Int32)' method in 'MvcDemo1.Controllers.EmployeeDataController'. The optional parameter must be a reference type, type null, or declared as an optional parameter. Parameter name: parameters
Any help would be helpful for my problem.
When I update my code and try to compile the program, there is no error at compile time, but when I run the program, I get the error
EmployeeData employee= edc.employeedata.Single(x=>x.id==id);
** Exception of type "System.Data.Entity.Core.EntityCommandExecutionException" occurred in EntityFramework.SqlServer.dll but was not handled in user code
Additional information: An error occurred while executing the command Definition **
source to share
When you visit a url, for example EmployeeData/details/1
, MVC will use your routes to map a controller, action method, and parameters to use. By default, the route will look something like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With your url it says that as a control method is EmployeeData
used details
as an action method and 1
as a value for an optional parameter named id
. Your parameter is named idd
so it doesn't appear. Since the parameter is marked as optional, but it is not null in your method details
, an exception is thrown.
You have three ways to solve this.
First, you can change the route to use idd
instead id
:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{idd}",
defaults: new { controller = "Home", action = "Index", idd = UrlParameter.Optional }
);
Second, you can change the parameter in your method details
to be called id
:
public ActionResult Details(int id)...
Finally, you can change the url you are using to be explicit in the parmater you send:
localhost:4166/EmployeeData/details?idd=1
source to share
If you are using the default route config - just rename your action parameter to id
:
public ActionResult Details(int id)
{
EmployeeDataContext edc = new EmployeeDataContext();
EmployeeData employee = edc.employeedata.Single(emp=>emp.id == id);
return View(employee);
}
Your parameter name id
must match exactly the one in your route config.
source to share