53

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
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
  • 9
    Do not correct your code based on the answer. The answer then makes no sense. I have rolled back your changes. –  Oct 30 '15 at 06:50

5 Answers5

81

It appears that you are using the default route which is defined as this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The key part of that route is the {id} piece. If you look at your action method, your parameter is k instead of id. You need to change your action method to this so that it matches the route parameter:

// change int k to int id
public ActionResult DetailsData(int id)

If you wanted to leave your parameter as k, then you would change the URL to be:

http://localhost:7317/Employee/DetailsData?k=4

You also appear to have a problem with your connection string. In your web.config, you need to change your connection string to this (provided by haim770 in another answer that he deleted):

<connectionStrings>
  <add name="EmployeeContext"
       connectionString="Server=.;Database=mytry;integrated security=True;"
       providerName="System.Data.SqlClient" />
</connectionStrings>
Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • Updated the answer with a fix for your connection string – Dismissile Apr 23 '14 at 13:27
  • i don't what is happing . now it showing One or more validation errors were detected during model generation: MVCViewDemo.Models.Employee: : EntityType 'Employee' has no key defined. Define the key for this EntityType. Employees: EntityType: EntitySet 'Employees' is based on type 'Employee' that has no keys defined. – Amit Kumar Apr 23 '14 at 13:35
  • @AmitKumar YOu don't have any configuration on your entity to specify what field is the primary key. Put the [Key] attribute on your EmpId field. – Dismissile Apr 23 '14 at 15:14
  • Is the convention used to match the URL parameter with the action parameter case sensitive? e.g. if the action parameter is "id" does the URL parameter also have to be "id", could it be "Id" or "ID" instead? The documentation here suggests that they should match http://msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx but there is nothing concrete written about this. – Ben Smith Jan 08 '15 at 11:41
7

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();
    }
Reza
  • 18,865
  • 13
  • 88
  • 163
  • FYI, I recently learned that for nullable types, you have access to `.Value` (the non-nullable value, for when you have casting issues) and `.HasValue`, which simplifies the null check you have to do :) **Edit** I missed that you already used `.Value`. – Flater Apr 23 '14 at 13:21
  • you are passing "2" with out any name either make the url localhost:7317/Employee/DetailsData?k=2 or modify the action to public ActionResult DetailsData(int? id) – Shanker Paudel Apr 23 '14 at 13:34
  • i did the same . this is my code public ActionResult DetailsData(int? id) { EmployeeContext Ec = new EmployeeContext(); if (id != null) { Employee emp = Ec.Employees.Single(X => X.EmpId == id.Value); return View(emp); } return View(); } but now error is "One or more validation errors were detected during model generation:" – Amit Kumar Apr 23 '14 at 13:45
  • 1
    @AmitKumar referring to default route mapping when you don't specify any parameters in your url it consider it as id, (unless you have changed that) so your param in action should be int? id. otherwise you should call it as http://localhost:7317/Employee/DetailsData?k=22 – Reza Apr 23 '14 at 15:10
4

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

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
0

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.

Manoj Naik
  • 386
  • 4
  • 18
0

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

irfandar
  • 1,690
  • 1
  • 23
  • 24