1

According to my previous question here: How to send a JSON object using GET method, I could retrieve my values because the object is no longer null. My object contains one DateTime property called CreatedOn. I am sending the value from javascript as new Date(). Before to answer that I can get the DateTime.Now() from code-behind, there is a purpose to send the date from HTML.

Then, in debug mode when I arrive to the controller method, my CreatedOn property is always DateTime.MinValue = 01/01/0001 12:00:00 AM

I changed my javascript value to this format "yyyyMMddT000000" because I thought that this would be parsed automatically but I didn't have any success.

How can I do to send the value that can be parsed by the web api2 controller automatically?

<script>
    $("#btnTest").on("click", function () {
        var searchCriteria = {};
        searchCriteria.ID = 0;
        searchCriteria.Name = "";
        //1. First tried option 
        //searchCriteria.CreatedOn = new Date();
        //2. Second tried option. Test
        searchCriteria.CreatedOn = "20170324T000000";

        var url = "http://localhost:8080/api/products"
        $.getJSON(url, searchCriteria).done(processResponse);
    });

    function processResponse(response){
    }
</script>
Community
  • 1
  • 1
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • the best approach for avoiding this is using timestamp – NicoRiff Mar 24 '17 at 16:59
  • Are you using CreatedOn as the name of the field to receive the date on your controller? – Zinov Mar 24 '17 at 17:00
  • @NicoRiff Thanks for your comment. Can you provide an example? I tried to do this also with no success. searchCriteria.CreatedOn = new Date().getTime() – Maximus Decimus Mar 24 '17 at 17:43
  • @Zinov, Yes. In the reference link you can view my full example. I have a Class that contains the DateTime property CreatedOn. All others values are mapped correctly. They only problem is the DateTime that I think cannot be parsed. – Maximus Decimus Mar 24 '17 at 17:45

1 Answers1

0

I got it. Hope this can help others.

searchCriteria.CreatedOn = new Date().toISOString();

This will be parsed automatically.

Cheers.

Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95