Unable to deserialize date in array of complex objects












0














On MVC side I have a class like this:



public class ComplexOne
{
public DateTime Date {get;set;}
public int Value {get;set;}
}


And in controller action



public virtual JsonResult TakeData(int id, ComplexOne data)  


From JS I'm sending object like this:



{
id = 10,
data = [
{Date:"2017-12-27", Value:10},
{Date:"2017-12-27", Value:20},
{Date:"2017-12-27", Value:30}
]
}


MVC understands all except Date, which deserializes as default value ({01.01.0001 0:00:00}). I've tried different date formats - yyyy-MM-dd, dd-MM-yyyy, MM/dd/yyyy and even ISO one, but got no luck.



How to do this in correct way without passing date as string and manual parsing in MVC?










share|improve this question
























  • Because in your class you have DateTime object but you are passing date as string to deserialize.
    – varatharajan
    yesterday












  • There's no problem in other cases; even more in ISO format the full date and time are passed.
    – Troll the Legacy
    yesterday










  • I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
    – varatharajan
    yesterday












  • @TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
    – Nkosi
    yesterday












  • @varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
    – Troll the Legacy
    yesterday


















0














On MVC side I have a class like this:



public class ComplexOne
{
public DateTime Date {get;set;}
public int Value {get;set;}
}


And in controller action



public virtual JsonResult TakeData(int id, ComplexOne data)  


From JS I'm sending object like this:



{
id = 10,
data = [
{Date:"2017-12-27", Value:10},
{Date:"2017-12-27", Value:20},
{Date:"2017-12-27", Value:30}
]
}


MVC understands all except Date, which deserializes as default value ({01.01.0001 0:00:00}). I've tried different date formats - yyyy-MM-dd, dd-MM-yyyy, MM/dd/yyyy and even ISO one, but got no luck.



How to do this in correct way without passing date as string and manual parsing in MVC?










share|improve this question
























  • Because in your class you have DateTime object but you are passing date as string to deserialize.
    – varatharajan
    yesterday












  • There's no problem in other cases; even more in ISO format the full date and time are passed.
    – Troll the Legacy
    yesterday










  • I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
    – varatharajan
    yesterday












  • @TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
    – Nkosi
    yesterday












  • @varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
    – Troll the Legacy
    yesterday
















0












0








0


1





On MVC side I have a class like this:



public class ComplexOne
{
public DateTime Date {get;set;}
public int Value {get;set;}
}


And in controller action



public virtual JsonResult TakeData(int id, ComplexOne data)  


From JS I'm sending object like this:



{
id = 10,
data = [
{Date:"2017-12-27", Value:10},
{Date:"2017-12-27", Value:20},
{Date:"2017-12-27", Value:30}
]
}


MVC understands all except Date, which deserializes as default value ({01.01.0001 0:00:00}). I've tried different date formats - yyyy-MM-dd, dd-MM-yyyy, MM/dd/yyyy and even ISO one, but got no luck.



How to do this in correct way without passing date as string and manual parsing in MVC?










share|improve this question















On MVC side I have a class like this:



public class ComplexOne
{
public DateTime Date {get;set;}
public int Value {get;set;}
}


And in controller action



public virtual JsonResult TakeData(int id, ComplexOne data)  


From JS I'm sending object like this:



{
id = 10,
data = [
{Date:"2017-12-27", Value:10},
{Date:"2017-12-27", Value:20},
{Date:"2017-12-27", Value:30}
]
}


MVC understands all except Date, which deserializes as default value ({01.01.0001 0:00:00}). I've tried different date formats - yyyy-MM-dd, dd-MM-yyyy, MM/dd/yyyy and even ISO one, but got no luck.



How to do this in correct way without passing date as string and manual parsing in MVC?







json ajax datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Tetsuya Yamamoto

14.5k41939




14.5k41939










asked yesterday









Troll the Legacy

317




317












  • Because in your class you have DateTime object but you are passing date as string to deserialize.
    – varatharajan
    yesterday












  • There's no problem in other cases; even more in ISO format the full date and time are passed.
    – Troll the Legacy
    yesterday










  • I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
    – varatharajan
    yesterday












  • @TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
    – Nkosi
    yesterday












  • @varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
    – Troll the Legacy
    yesterday




















  • Because in your class you have DateTime object but you are passing date as string to deserialize.
    – varatharajan
    yesterday












  • There's no problem in other cases; even more in ISO format the full date and time are passed.
    – Troll the Legacy
    yesterday










  • I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
    – varatharajan
    yesterday












  • @TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
    – Nkosi
    yesterday












  • @varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
    – Troll the Legacy
    yesterday


















Because in your class you have DateTime object but you are passing date as string to deserialize.
– varatharajan
yesterday






Because in your class you have DateTime object but you are passing date as string to deserialize.
– varatharajan
yesterday














There's no problem in other cases; even more in ISO format the full date and time are passed.
– Troll the Legacy
yesterday




There's no problem in other cases; even more in ISO format the full date and time are passed.
– Troll the Legacy
yesterday












I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
– varatharajan
yesterday






I think it expects date with time in the string. or try creating DateTime Object like {Date: new DateTime(2015,06,27), Value: 10}
– varatharajan
yesterday














@TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
– Nkosi
yesterday






@TrolltheLegacy Is that class shown exactly as how you have in your code? The shown class is private and has private fields. I am uncertain that what is shown binds to anything. Provide a Minimal, Complete, and Verifiable example that represents the actual problem.
– Nkosi
yesterday














@varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
– Troll the Legacy
yesterday






@varatharajan Then browser sends ugliest datetime string I've seen and again with no luck.
– Troll the Legacy
yesterday














1 Answer
1






active

oldest

votes


















0














You are passing Date as string in the JSON object but your Date in ComplexOne model class is DateTime.



So you can use DTO to receive the JSON object in the controller action and then convert the DTO to actual model as follows:



public class ComplexOneDto
{
public string Date {get;set;}
public int Value {get;set;}
}


The controller action:



public virtual JsonResult TakeData(int id, ComplexOneDto data)
{
// here convert the ComplexOneDto to ComplexOne as follows

List<ComplexOne> complexOnes = new List<ComplexOne>();
foreach (ComplexOneDto complexOneDto in data)
{
DateTime convertedDateTime = DateTime.ParseExact(complexOneDto.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

complexOnes .Add(new ComplexOne() { Date = convertedDateTime, Value = complexOneDto.Value });
}

return Json(true);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943313%2funable-to-deserialize-date-in-array-of-complex-objects%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You are passing Date as string in the JSON object but your Date in ComplexOne model class is DateTime.



    So you can use DTO to receive the JSON object in the controller action and then convert the DTO to actual model as follows:



    public class ComplexOneDto
    {
    public string Date {get;set;}
    public int Value {get;set;}
    }


    The controller action:



    public virtual JsonResult TakeData(int id, ComplexOneDto data)
    {
    // here convert the ComplexOneDto to ComplexOne as follows

    List<ComplexOne> complexOnes = new List<ComplexOne>();
    foreach (ComplexOneDto complexOneDto in data)
    {
    DateTime convertedDateTime = DateTime.ParseExact(complexOneDto.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

    complexOnes .Add(new ComplexOne() { Date = convertedDateTime, Value = complexOneDto.Value });
    }

    return Json(true);
    }





    share|improve this answer




























      0














      You are passing Date as string in the JSON object but your Date in ComplexOne model class is DateTime.



      So you can use DTO to receive the JSON object in the controller action and then convert the DTO to actual model as follows:



      public class ComplexOneDto
      {
      public string Date {get;set;}
      public int Value {get;set;}
      }


      The controller action:



      public virtual JsonResult TakeData(int id, ComplexOneDto data)
      {
      // here convert the ComplexOneDto to ComplexOne as follows

      List<ComplexOne> complexOnes = new List<ComplexOne>();
      foreach (ComplexOneDto complexOneDto in data)
      {
      DateTime convertedDateTime = DateTime.ParseExact(complexOneDto.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

      complexOnes .Add(new ComplexOne() { Date = convertedDateTime, Value = complexOneDto.Value });
      }

      return Json(true);
      }





      share|improve this answer


























        0












        0








        0






        You are passing Date as string in the JSON object but your Date in ComplexOne model class is DateTime.



        So you can use DTO to receive the JSON object in the controller action and then convert the DTO to actual model as follows:



        public class ComplexOneDto
        {
        public string Date {get;set;}
        public int Value {get;set;}
        }


        The controller action:



        public virtual JsonResult TakeData(int id, ComplexOneDto data)
        {
        // here convert the ComplexOneDto to ComplexOne as follows

        List<ComplexOne> complexOnes = new List<ComplexOne>();
        foreach (ComplexOneDto complexOneDto in data)
        {
        DateTime convertedDateTime = DateTime.ParseExact(complexOneDto.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

        complexOnes .Add(new ComplexOne() { Date = convertedDateTime, Value = complexOneDto.Value });
        }

        return Json(true);
        }





        share|improve this answer














        You are passing Date as string in the JSON object but your Date in ComplexOne model class is DateTime.



        So you can use DTO to receive the JSON object in the controller action and then convert the DTO to actual model as follows:



        public class ComplexOneDto
        {
        public string Date {get;set;}
        public int Value {get;set;}
        }


        The controller action:



        public virtual JsonResult TakeData(int id, ComplexOneDto data)
        {
        // here convert the ComplexOneDto to ComplexOne as follows

        List<ComplexOne> complexOnes = new List<ComplexOne>();
        foreach (ComplexOneDto complexOneDto in data)
        {
        DateTime convertedDateTime = DateTime.ParseExact(complexOneDto.Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

        complexOnes .Add(new ComplexOne() { Date = convertedDateTime, Value = complexOneDto.Value });
        }

        return Json(true);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        TanvirArjel

        3,74821538




        3,74821538






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943313%2funable-to-deserialize-date-in-array-of-complex-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas