Praneeth Podduturi
Praneeth Podduturi

Reputation: 5

When i run the code i am getting this error

I am getting this when trying to do using asp.net mvc

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(Int32)' in 'Contr1.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

namespace Contr1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ViewResult Index(int id)
        {
            EmployeeBusinessLayer employeeBL = new EmployeeBusinessLayer();
            Employee employee = employeeBL.GetEmployeDetails(175);

            ViewBag.Employee = employee; 
            ViewBag.Header = "Employee Details";
            return View();
        }
    }
}

namespace Contr1.Models
{
    public class EmployeeBusinessLayer
    {
        public Employee GetEmployeDetails(int EmployeeId)
        {
            Employee employee = new Employee()
            {
                Id = EmployeeId,
                Name = "aaaa",
                Gender = "Male",
                Address = "Alwal",
                City = "Hyderabad",
                Salary = 56431,
            };
            return employee;
        }
    }
}

I need to get the details on the web output but i am getting value to nullable or referenced or optional parameter

Upvotes: 0

Views: 55

Answers (1)

Leon
Leon

Reputation: 919

You get this error because you require an id as parameter

public ViewResult Index(int id)

Although you don't use it at this point you still require it. Either remove the "int id" or make the id nullable like this:

public ViewResult Index(int? id)

Upvotes: 1

Related Questions