How to pass datetime from view to controller in asp.net MVC

前端 未结 2 1467
南方客
南方客 2021-01-12 15:16

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

Edited



        
2条回答
  •  没有蜡笔的小新
    2021-01-12 15:51

    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:

    
    
    [HttpPost]
    public ActionResult Index(Student[] students)
    {
      ...
    }
    

提交回复
热议问题