How to append datetime value to formdata and receive it in controller

Guruprasad J Rao

I want to know how I can pass datetime value through formdata and retrieve it in controller and convert it to DateTime in the controller

I've tried as below:

var formdata=new FormData();
fromDate = $('.from_date').datepicker('getUTCDate');
toDate = $('.to_date').datepicker('getUTCDate');

formdata.append("start", new Date(fromDate));
formdata.append("end", new Date(toDate));

and in $.ajax I am setting data:formdata

in my controller I receive it as below:

DateTime frmDate = Convert.ToDateTime(Request.Form["start"]).Date;
DateTime toDate = Convert.ToDateTime(Request.Form["end"]).Date;

But here I get a System.FormatException while trying to Convert to datetime and when I keep a watch for Request.Form["start"] then the value will be "Fri Mar 30 2015 05:30:00 GMT+0530 (Indian Standard Time)" but it considers it as string when retrieving from request.

Is it possible to pass datetime type through Request?

Jasen

You get FormatException because the date string is not formatted in recognized pattern for the .NET date parser. If we can be more specific about the format in javascript we can satisfy the .NET parser.

var datestr = (new Date(fromDate)).toUTCString();
formdata.append("start", datestr);

Either of these will give us an accepted format

Now we parse the string in your server-side code

DateTime fromDate = Convert.ToDateTime(Request.Form["start"]).Date;

Depending on your machine's culture settings you may need to use DateTime.ParseExact() instead of Convert.ToDateTime().

Is it possible to pass datetime type through Request?

Along the pipeline from javascript to your controller action this will be converted to a string or integer anyway. We could return the tick (milisecond) representation for the DateTime but then you'd need to convert that to .NET ticks which uses a different epoch and nanosecond units.

Just stick to strings with standard formats.

More on parsing Date formats here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to receive html check box value in spring mvc controller

From Dev

how to receive html check box value in spring mvc controller

From Dev

How can I receive form data in callback with post value in controller?

From Dev

How do I append a value to formData on event? (blueimp/jQuery-File-Upload)

From Dev

Append Value to $_POST Array with jQuery FormData.append

From Dev

How to show value from controller in html element added with append() method?

From Dev

How to append an image from URL to a FormData - Javascript

From Dev

How to append multiple files into formdata using jquery

From Dev

How to POST a DateTime value to a Web API 2 controller

From Dev

How to remove value from FormData

From Dev

submit form ajax, receive null value in controller

From Dev

Why my formdata.append is not sending key value pair to server?

From Dev

$.each unable to append data to formData() , in spite of key value being populated

From Dev

Append object which contain File and String value into FormData - JS

From Dev

How can i receive a FormData with a file and string appended in PHP?

From Dev

How to post a value to controller to receive Data back to view Angular 5+?

From Java

How to append whole set of model to formdata and obtain it in MVC

From Dev

How to append multiple file inputs to a FormData object using $.each?

From Dev

How to pass 'filename' parameter to FormData.append() method for string data

From Dev

How can I use the new FormData methods apart from append?

From Dev

How to receive dynamic data in WebAPI controller

From Dev

How to receive a ajax response from a codeigniter controller?

From Dev

Append var to FormData

From Dev

Append selected option to FormData

From Dev

Cant append elements to FormData

From Dev

Append data to formData object

From Dev

Checkboxes with FormData.Append

From Dev

How to append the value in table

From Dev

How to send single value along with Formdata

Related Related

  1. 1

    how to receive html check box value in spring mvc controller

  2. 2

    how to receive html check box value in spring mvc controller

  3. 3

    How can I receive form data in callback with post value in controller?

  4. 4

    How do I append a value to formData on event? (blueimp/jQuery-File-Upload)

  5. 5

    Append Value to $_POST Array with jQuery FormData.append

  6. 6

    How to show value from controller in html element added with append() method?

  7. 7

    How to append an image from URL to a FormData - Javascript

  8. 8

    How to append multiple files into formdata using jquery

  9. 9

    How to POST a DateTime value to a Web API 2 controller

  10. 10

    How to remove value from FormData

  11. 11

    submit form ajax, receive null value in controller

  12. 12

    Why my formdata.append is not sending key value pair to server?

  13. 13

    $.each unable to append data to formData() , in spite of key value being populated

  14. 14

    Append object which contain File and String value into FormData - JS

  15. 15

    How can i receive a FormData with a file and string appended in PHP?

  16. 16

    How to post a value to controller to receive Data back to view Angular 5+?

  17. 17

    How to append whole set of model to formdata and obtain it in MVC

  18. 18

    How to append multiple file inputs to a FormData object using $.each?

  19. 19

    How to pass 'filename' parameter to FormData.append() method for string data

  20. 20

    How can I use the new FormData methods apart from append?

  21. 21

    How to receive dynamic data in WebAPI controller

  22. 22

    How to receive a ajax response from a codeigniter controller?

  23. 23

    Append var to FormData

  24. 24

    Append selected option to FormData

  25. 25

    Cant append elements to FormData

  26. 26

    Append data to formData object

  27. 27

    Checkboxes with FormData.Append

  28. 28

    How to append the value in table

  29. 29

    How to send single value along with Formdata

HotTag

Archive