Search code examples
c#asp.net-mvcabp-framework

How to pass empty string to nullable int


I have this property

public int? CodigoBanco { get; set; }

An when i try to send it to server in this way

codigoBanco: ""

I have the following error

The following errors were detected during validation:\r\n - The JSON value could not be converted to System.Nullable`1[System.Int32]


Solution

  • You have two ways to resolve the problem:

    1. Specify null (not empty string) and it will be successfully mapped. codigoBanco: null or remove this property at all from client-side(the default will be the same null)

    2. Write custom convertaion at backend-side, something like:

      CodigoBanco = int.Parse(codigoBanco == "" || codigoBanco.Length = 0 ? null : codigoBanco);