chetan kambli
chetan kambli

Reputation: 814

MVC issue for accessing the data from database:The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

I am working on MVC where I am trying to access data from the database. I am new to MVC. I am getting the following error:

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 'MVCDemo1.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

Code:

Database.SetInitializer<MVCDemo1.Models.EmployeeContext>(null);

I have added the above line in global.asax.cs file and I am getting the error there.

The below is my Model:Employee.cs

namespace MVCDemo1.Models
{
    [Table("tblEmployee")]
    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }
}

The below is my code for the Controller:EmployeeController

public ActionResult Details(int id)
        {                    
                EmployeeContext employeeContext = new EmployeeContext();
                Employee employee = employeeContext.Employees.Single(x =>           x.EmployeeId == id);

                return View(employee);
                            
    }

The below code is for View:details.cshtml

@model MVCDemo1.Models.Employee

    @{
        ViewBag.Title = "Employee Details";
    }
    
    <h2>Employee Details</h2>
    
    <table style="font-family:Arial">
        <tr>
            <td>
                Employee ID:
            </td>
            <td>
                @Model.EmployeeId
            </td>
        </tr>
        <tr>
            <td>
                Name:
            </td>
            <td>
                @Model.Name
            </td>
        </tr>
        <tr>
            <td>
                Gender:
            </td>
            <td>
                @Model.Gender
            </td>
        </tr>
        <tr>
            <td>
                City:
            </td>
            <td>
                @Model.City
            </td>
        </tr>
    </table>

My Route configPath is:

defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }

Can anyone enlighten me with this error?

I tried by removing id = UrlParameter.Optional from routeconfig but not worked for me.

Upvotes: 1

Views: 134

Answers (3)

One more thing: I would not just create a new instance of your EmployeeContext. You could get this using dependency injection.

public class MyController
{
    private readonly EmployeeContext _context;

    public MyController(EmployeeContext context)
    {
      _context = context;
    }

    ...
}

Check out Microsoft's documentation on this:

https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

Upvotes: 0

Tsahi Asher
Tsahi Asher

Reputation: 1812

The error is coming from the MVC framework, and has nothing to do with your database configuration. As the error states, you have a non-nullable argument for your action, an int, and you are not sending a value for it. This can happen if you make HTTP calls to this action without providing a value for id, e.g. through a query string in the URL, or otherwise invoking the method. Leaving id = UrlParameter.Optional lets you do that. If you remove this, you might see another error, suggesting where this is coming from.

Upvotes: 2

Vivek
Vivek

Reputation: 174

if your id parameter can be null then you have to make it nullable type and check before querying employee table

 public ActionResult Details(int? id)
    { 
     if(id != null)
      {                   
       EmployeeContext employeeContext = new EmployeeContext();
       Employee employee = employeeContext.Employees.Single(x =>x.EmployeeId == id);

            return View(employee);
     }
    else {
      return  SomeView();
     }

}

Upvotes: 1

Related Questions