[c#] MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32'

I am new to MVC. In My Application , I'm Retrieving the Data from Mydatabase. but when I run my Application it show Error Like This this is my url

http://localhost:7317/Employee/DetailsData/4



  Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult DetailsData(Int32)' in 'MVCViewDemo.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

this is my web.config file code

<connectionStrings>
  <add name="EmployeeContext" connectionString="Server=.;Database=mytry;integrated security=true; ProviderName=System.Data.SqlClient"/>
</connectionStrings>

this is my Employee Model Class(Employee.cs)

[Table("emp")]    /* to map tablename with our class name*/
    public class Employee
    {
        public int EmpId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }

    }

My EmployeeContext.cs Model class

 public class EmployeeContext:DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }

my EmployeeController.cs

 public ActionResult DetailsData(int k)
        {

            EmployeeContext Ec = new EmployeeContext();
            Employee emp = Ec.Employees.Single(X => X.EmpId == k);           
            return View(emp);
        }

and my view

<h2>DetailsData</h2>
@Model.Name<br />
@Model.City<br />
@Model.Gender<br />
@Model.EmpId

This question is related to c# asp.net-mvc asp.net-mvc-4

The answer is


It seems that your action needs k but ModelBinder can not find it (from form, or request or view data or ..) Change your action to this:

public ActionResult DetailsData(int? k)
    {

        EmployeeContext Ec = new EmployeeContext();
        if (k != null)
        {
           Employee emp = Ec.Employees.Single(X => X.EmpId == k.Value);

           return View(emp);
        }
        return View();
    }

Also make sure the value is not too large or too small for int like in my case.


I am also new to MVC and I received the same error and found that it is not passing proper routeValues in the Index view or whatever view is present to view the all data.

It was as below

<td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>

I changed it to the as show below and started to work properly.

<td>
            @Html.ActionLink("Edit", "Edit", new { EmployeeID=item.EmployeeID }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>

Basically this error can also come because of improper navigation also.


I faced this error becouse I sent the Query string with wrong format

http://localhost:56110/user/updateuserinfo?Id=55?Name=Basheer&Phone=(111)%20111-1111
------------------------------------------^----(^)-----------^---...
--------------------------------------------must be &

so make sure your Query String or passed parameter in the right format


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core

Examples related to asp.net-mvc-4

Better solution without exluding fields from Binding How to remove error about glyphicons-halflings-regular.woff2 not found When should I use Async Controllers in ASP.NET MVC? How to call controller from the button click in asp.net MVC 4 How to get DropDownList SelectedValue in Controller in MVC Return HTML from ASP.NET Web API There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key country Return JsonResult from web api without its properties how to set radio button checked in edit mode in MVC razor view How to call MVC Action using Jquery AJAX and then submit form in MVC?