Ticket (Solved)

null integer parameter question

This may or may not be specific to orchard but here goes. I have a controller with the following two actions

   public string test1(int nEvent)
    {  return nEvent.ToString(); }

    public string test2(int? nEvent)
    { return "nEvent = " + nEvent.ToString(); }

so I would call the first one as follows:

http://localhost:30321/OrchardLocal/mymodule/event/test1/123

I get an exception thrown in the logs System.ArgumentException: The parameters dictionary contains a null entry for parameter 'nEvent' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'mymodule.Controllers.eventController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

since it mentions it being null I tried the test two http://localhost:30321/OrchardLocal/mymodule/mycontroller/test2/123

and it writes the string out but nothing for the number 123 that I passed into the function.

So now my question, why does this work for other functions in my controller and now here, am I missing something basic that would cause this? I hope I am just a dopey noob and someone can easily point me in the right direction and earn some easy points off me.

Thanks in advance,

Re: null integer parameter question

Ok, so here is what I found out for those who care. As you can see the function signature uses nEvent as the parameter name but because of using a routing pattern that is something like Module/Controller/Action/Id, the parameter actually has to be named Id. So somewhere in orchard that is defined by default and the functions that were working had that parameter named Id. I would love to assign these points to someone who can show me how to customize the route for a particular controller if anyone is up for it?

Friday, May 30, 2014 2:28:15 AM byRob Allred

Re: null integer parameter question

The following route will handle anything that starts with "MyModule" and works with an optional "eventId" instead of the default "id" :

public IEnumerable<RouteDescriptor> GetRoutes() { yield return new RouteDescriptor { Route = new Route( "MyModule/{controller}/{action}/{eventId}", new RouteValueDictionary { {"area", "MyModule"}, {"eventId", UrlParameter.Optional} }, new RouteValueDictionary(), new RouteValueDictionary { {"area", "MyModule"} }, new MvcRouteHandler()) } }; }

And of course you can completely change the URL pattern, e.g.:

public IEnumerable<RouteDescriptor> GetRoutes() { yield return new RouteDescriptor { Route = new Route( "eventlog/{eventId}", new RouteValueDictionary { {"area", "MyModule"}, {"controller", "MyController"}, {"action", "MyAction"}, {"eventId", UrlParameter.Optional} // Or if eventId should not be optional, simply get rid of this line and don't accept a nullable eventId on your controller action. }, new RouteValueDictionary(), new RouteValueDictionary { {"area", "MyModule"} }, new MvcRouteHandler()) } }; }

Hope this helps.

Sunday, June 1, 2014 1:46:49 PM bysfmskywalker

Post a reply

You need to be signed in to post a reply.

Sign In