6

Am trying to pass below data form my view to controller.

Edited

<script type="text/javascript">
    var pathname = 'http://' + window.location.host;
  var Student = [
  { Name: "Vijay", ID: 1, DOB: "2010-12-09T08:00:00.000Z" },
  { Name: "Anand", ID: 2, DOB: "2010-12-09T08:00:00.000Z" }
  ];

  $.ajax({
    url: pathname + "/Home/UpadetStu",
    type: "POST",
    dataType: "json",
    data: JSON.stringify(Student),
    contentType: "application/json; charset=utf-8",
    success: function (result) { }, 
    failure: function (r, e, s) { alert(e); } 
  });

</script>



   [ObjectFilter(Param = "stuData", RootType = typeof(Stu[]))]
    public JsonResult UpadetStu(Stu[] stuData)
    {
        return this.Json(new { success = true });
    }

[DataContract]
public class Stu
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public DateTime? DOB { get; set; }

}

But in the controller am getting null for Name and ID , default datetime for DOB, I found that there is problem in passing datetime. Is there any better way to pass datetime from view to controller? do i miss any parsing?

6
  • What does your Action on your controller look like? Commented Jun 25, 2012 at 8:56
  • [ObjectFilter(Param = "studentData", RootType = typeof(Stu[]))] public JsonResult UpadetStudent(Stu[] studentData) { return this.Json(new { success = true }); } Am using above code in controller. Commented Jun 25, 2012 at 9:00
  • What does your Stu object look like then? does it have a DOB Property? Have you verified what your jQuery.ajax is actually posting to your server, using firebug or fiddler? Commented Jun 25, 2012 at 9:08
  • This is how it look like. [DataContract] public class Stu { [DataMember] public string Name { get; set; } [DataMember] public int ID { get; set; } [DataMember] public DateTime DOB{ get; set; } } Commented Jun 25, 2012 at 9:49
  • I just copied your code and tried and it is working just fine.. the only difference is I don't have that ObjectFilter. Commented Jun 25, 2012 at 15:37

2 Answers 2

6

The problem is Thu Dec 9 13:30:00 UTC+0530 2010 can't be parsed into a valid datetime object in c#. You can try that by simply calling DateTime.Parse("Thu Dec 9 13:30:00 UTC+0530 2010") it will fail.

I would suggest that instead of returning that date format from the server you can better return the ISO 8601 format that looks like 2010-12-09T08:00:00.000Z.

You can easily convert the long datetime format into ISO 8601 from javascript by,

new Date("Thu Dec 9 13:30:00 UTC+0530 2010").toJSON();

If you are using JSON.NET library you can easily control the way in which the datetimes have to be serialized.

UPDATE:

<script type="text/javascript">

  var Student = [
  { Name: "Vijay", ID: 1, DOB: "2010-12-09T08:00:00.000Z" },
  { Name: "Anand", ID: 2, DOB: "2010-12-09T08:00:00.000Z" }
  ];

  $.ajax({
    url: "/Home/Index",
    type: "POST",
    dataType: "json",
    data: JSON.stringify(Student),
    contentType: "application/json; charset=utf-8",
    success: function (result) { }, 
    failure: function (r, e, s) { alert(e); } 
  });

</script>

[HttpPost]
public ActionResult Index(Student[] students)
{
  ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

var studentString = JSON.stringify(Student); itself will change format, "studentString" will have DOB in this format 2010-12-09T08:00:00.000Z only then why its not working?
I just tested with a simple textbox entering the datetime in ISO format and it worked fine
Please post more code then I can help to figure out where the issue is
By the way why you want to stringify the data? you can just use it as a plain object in $.post right?
I have posted all my code in the above question and comments. For some reason i don't want to use $.post. Please help me how to make this work by using above code.
|
0

If your studentData object in your controller is null, the JSON.stringify(Student) is producing an object that is not proper JSON or an object that can not be parsed to your Stu object.

Verify that your JS Student object is correct, then verify the JSON you produce doing JSON.stringify

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.