0

I am receiving a JSON from the request and able to bind to the model successfully, But when the value of any property in JSON is null, the model fails to bind. Below is my code

 public class JobDetails
        {
            [Required]
            [Range(1, int.MaxValue, ErrorMessage = "TransactionId value must be greater than zero")]
            public int Id { get; set; }        
            public string details{ get; set; }
        } 

    [HttpPost]
    [Route("create/job")]
    public async Task<IActionResult> Add([FromBody]JobDetails model)
    {
      //when i debug model is null. Expected result {Id:null,details:"some data"}
    }

input json

{Id:null,details:"some data"}

Any idea of how to make this work?

2 Answers 2

1

You can use the below attribute for ignoring null checking:

 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  public int Id { get; set; } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing your integer to

public int? Id { get; set; }

which accepts null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.