0

My controller action expects several arguments, but only one can be nullable on POST. It is of type decimal. Based on the research I've done for default routing, it looks limited to setting a default value for an action expecting a single parameter.

This is the response message I get due to this:

MVC : The parameters dictionary contains a null entry for parameter 'z' of non-nullable type 'System.Decimal'

Here is the method signature in BarController.cs:

[HttpPost]
public PartialViewResult Bar(int x, string y, decimal z) {*/Etc*/}

Here is the route I attempted but was not successful (RouteConfig.cs):

routes.MapRoute(name: "Bar", url: "Foo/Bar/z",
defaults: new {controller = "Foo", action = "Bar", z= 0 });

My request body has values for x and z, but y can be nullable.

How do I refactor so that z can be nullable? Apart from literally making it a nullable decimal type. I already have algorithms that subscribe to the Decimal type, not the nullable variant.

operationcwl
  • 11
  • 1
  • 6

3 Answers3

0

Sounds like you try to pass null as an int32, that will not work. In c# int32 is a value type, not a reference. You could use nullable int32, but reading your question it sounds like you were expecting something different then an int32.

Add some code, and you will get help!

Jocke
  • 2,189
  • 1
  • 16
  • 24
0

Use a nullable parameter like decimal? or int? in your action.

0

You can do your "int" or "decimal" parameters Nullable as well.

public PartialViewResult Bar(string y, decimal? z, int? x) {*/Etc*/}

Notice that in this case, you will have to validate the input in order of checking if "x" or "y" have values. In Check an integer value is Null in c# you can find some ways of validating a Nullable integer variable. The same approach could be used for checking if the decimal parameter is null.

javier_el_bene
  • 450
  • 2
  • 10
  • 25