2

I am facing issue while passing null parameter values to properties of my model in HttpGet verb.

I am using .Net Core 2.1 for my web API project. Below is my action method in controller:

    [HttpGet("get")]
    public ActionResult GetData([FromQuery]MyTestModel model)
    {
        var result = new MyTestModel();
        return new JsonResult(result);
    }

And my MyTestModel.cs is like :

 [Serializable]
public class MyTestModel
{
    public MyTestModel()
    {
        PageNo = 1;
        PageSize = 10;
    }
    public int ClientId { get; set; }
    public int? CandidateId { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public int PageNo { get; set; }
    public int PageSize { get; set; }
}

When I call the API like :

api/controller/get?clientId=7583&candidateId=null&fromDate=null&toDate=null

I am getting 400 response. Below is the response message:

{"toDate":["The value 'null' is not valid for ToDate."],
 "fromDate":["The value 'null' is not valid for FromDate."],
 "candidateId":["The value 'null' is not valid for CandidateId."]
}

When I don't send nullable properties at all(candidateId, fromDate,toDate), this hits my action and uses default values as null.

What's the problem if I am trying to explicitly setting null values?

Do I need to set some configuration in my Startup.cs to handle null values for nullable properties?

Any help will be appreciated .

Thanks in advance.

4
  • 2
    You need to just omit the value (and everything is sent as text, and converted to the type by the ModelBinder - there is no conversion from "null" to null) Commented Jul 9, 2018 at 9:40
  • 1
    You don't need to do fromDate=null. You just don't pass the values if you want to send them as null. When you do fromDate=null it takes "null" as string value and tries to assign to the property. that's why you are getting this error. Commented Jul 9, 2018 at 9:44
  • @StephenMuecke, is there any way to handle null values? I am just curious to know the solution. I knew the alternative by just omitting the null values from request. Commented Jul 9, 2018 at 9:46
  • Do you mean by using &candidateId=null? You would need to create a custom ModelBinder, (or a ValueProvider might also be an option). Note also an empty string translates to null Commented Jul 9, 2018 at 9:55

1 Answer 1

7

Everything sent in the query string is just a string. So, when you do something like toDate=null, you're actually saying "set toDate to "null"", i.e. the string "null". The modelbinder attempts to convert all the strings to the actual types you're binding to, but there's no conversion available that can turn "null" into a null DateTime.

To set the value to null, you need to either pass no value toDate= or just omit the key entirely from the query string.

Sign up to request clarification or add additional context in comments.

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.