Paul
Paul

Reputation: 3253

The parameters dictionary contains a null entry for parameter but parameter exists

I have a simple method in my controller - GetMeterProfileData with 1 parameter - meterID

public class AlertsDashboardController : Controller
{
    public ActionResult GetMeterProfileData(int meterID)
    {
        List<IMeterProfileData> result = new List<IMeterProfileData>();

        {
            if (meterID == 1)
            {
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("01/01/2000 00:00"), Consumption = 102});
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("31/01/2000 00:30"), Consumption = 12});
            }
            else
            {
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("01/01/2004 00:00"), Consumption = 500});
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("31/01/2004 00:30"), Consumption = 445});
            }
        }

        return Json(result, JsonRequestBehavior.AllowGet);

    }

}

I am using the URL

http://localhost:54728/AlertsDashboard/GetMeterProfileData/2

As you can see, MeterID is being specified

So why do I get the error

The parameters dictionary contains a null entry for parameter 'meterID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult GetMeterProfileData(Int32)' in 'STC.MVC.Web.Controllers.AlertsDashboardController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

My routing is set up as below

Do I have to add a new route for this method? That wouldnt make much sense to have to do that because I would have to set up loads of routes potentially.

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
            );

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

I also try adding a new route as below but that didnt work either

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

What am I missing?

Paul

Upvotes: 2

Views: 84

Answers (1)

dotnetom
dotnetom

Reputation: 24901

Parameter name matters and it must be id when using default routes. You need to either update your method signature to

public ActionResult GetMeterProfileData(int id)

Or define a different route with the correct parameter name:

routes.MapRoute(
        name: "Default2",
        url: "{controller}/{action}/{meterID}",
        defaults: new { controller = "AlertsDashboard", action = "Index", meterID = UrlParameter.Optional }
        );

Upvotes: 3

Related Questions