1

I need to know how to account for an argument to my method being null instead of an int .

I have this method which is supposed to receive an int

    [HttpGet]
    public ActionResult UpdateUser(int userId){ }

However when ever the session times out and I log back in while in the middle of calling this method it receives a null instead of an int.

This causes this error:

The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult UpdateUser(Int32)' in 'Controllers.UserController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I need a way to account for receiving a null argument.

So far I have tried overloading the method with a nullable int type like this

    [HttpGet]
    public ActionResult UpdateUser(int? userId){ }

but this just ends up receiving every UpdateUser() call and converting the value to null.

Any suggestions?

Joey Phillips
  • 1,543
  • 2
  • 14
  • 22
  • You don't need the first method. The second method will handle both situations...a null or an int. If its a null...redirect to... what...a login screen? – Chris Catignani May 02 '19 at 15:26
  • When I have the second method implemented it seems to override the first method on every call. Its purpose is to catch events where the int value is lost due to a session timeout, however when implemented it overrides the UpdateUser(int userId) method and the value is always converted to a null. The purpose is to catch the uncommon event that would error out and yes, redirect to a new page. – Joey Phillips May 02 '19 at 15:33
  • After re-reading your comment I see you said I don't need the first method, however the problem is that the second method seems to be converting the value to null where the first method does not. – Joey Phillips May 02 '19 at 15:35
  • The parameter your passing needs to be int? also. – Chris Catignani May 02 '19 at 16:02

2 Answers2

2

The signature for the overload, which won't be supplied the userId is probably:

[HttpGet]
public ActionResult UpdateUser(){ }

i.e. no userId was supplied, rather than it being supplied with a null value.

I say "probably" as it is possible that your request does in fact have the parameter/argument specified with no value.

If this is the case, you should probably use just the one signature:

[HttpGet]
public ActionResult UpdateUser(int? userId){ }

With a check...

if (userId.HasValue)
{
    // ok... you can use userId.Value
}
else
{
    // not ok...
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • I tired both of these methods here and it appears when I use your first solution I get this error "The current request for action 'UpdateUser' on controller type 'UserController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult UpdateUser() on type Controllers.UserController System.Web.Mvc.ActionResult UpdateUser(Int32) on type Controllers.UserController" – Joey Phillips May 02 '19 at 14:54
  • When I use your second solution I get a null value sent to userId no matter what. It doesn't error out, but it gets a null value when it should be getting an int value. – Joey Phillips May 02 '19 at 14:55
  • 1
    This ended up being the solution. Your second solution worked for me after some time working with it. Thank you. – Joey Phillips May 02 '19 at 20:10
0

Could you not simply use the signature

[HttpPost]
public ActionResult UpdateUser(int? userId = 0){ }

This should check to see if it is null and if it is, set it to 0.

JamesS
  • 2,167
  • 1
  • 11
  • 29