如何在asp.net MVC中将datetime从视图传递到控制器

时间:2022-12-02 15:04:07

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?

但是在控制器中,对于名称和ID,DOB的默认日期时间变为空,我发现传递datetime时出现问题。有没有更好的方法将datetime从视图传递到控制器?我想念任何解析吗?

2 个解决方案

#1


5  

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.

问题是Thu Dec 9 13:30:00 UTC + 0530 2010无法解析为c#中的有效日期时间对象。您可以通过简单地调用DateTime.Parse(“Thu Dec 9 13:30:00 UTC + 0530 2010”)来尝试它,它将失败。

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.

我建议您不要从服务器返回该日期格式,而是可以更好地返回看起来像2010-12-09T08:00:00.000Z的ISO 8601格式。

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

您可以轻松地将长日期时间格式从javascript转换为ISO 8601,

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.

如果您使用的是JSON.NET库,则可以轻松控制日期时间的序列化方式。

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)
{
  ...
}

#2


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.

如果控制器中的studentData对象为null,则JSON.stringify(Student)生成的对象不是正确的JSON或无法解析为Stu对象的对象。

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

验证您的JS Student对象是否正确,然后验证您使用JSON.stringify生成的JSON

#1


5  

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.

问题是Thu Dec 9 13:30:00 UTC + 0530 2010无法解析为c#中的有效日期时间对象。您可以通过简单地调用DateTime.Parse(“Thu Dec 9 13:30:00 UTC + 0530 2010”)来尝试它,它将失败。

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.

我建议您不要从服务器返回该日期格式,而是可以更好地返回看起来像2010-12-09T08:00:00.000Z的ISO 8601格式。

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

您可以轻松地将长日期时间格式从javascript转换为ISO 8601,

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.

如果您使用的是JSON.NET库,则可以轻松控制日期时间的序列化方式。

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)
{
  ...
}

#2


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.

如果控制器中的studentData对象为null,则JSON.stringify(Student)生成的对象不是正确的JSON或无法解析为Stu对象的对象。

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

验证您的JS Student对象是否正确,然后验证您使用JSON.stringify生成的JSON