如何追加日期时间价值FORMDATA和接受它的控制器 [英] how to append datetime value to formdata and receive it in controller

查看:656
本文介绍了如何追加日期时间价值FORMDATA和接受它的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎样才能FORMDATA传递日期时间价值,   检索它的控制器,并将其转换为DateTime在控制器

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

所以,按我的问题,我已经试过如下,但并没有帮助

So as per my question I've tried as below but did not help

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));

$ AJAX 我设置数据: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;

但在这里,我得到下面的 System.FormatException 试着转换为日期时间,当我把一个手表的Request.Form [开始] 则该值将是周五2015年3月30号5时30分00秒GMT + 0530(印度标准时间),但它要求检索时,认为它作为字符串。

But here I get below 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.

是否有可能通过DateTime类型通过请求?

推荐答案

您获得出现FormatException因为日期字符串不承认图案为.NET日期解析器格式化。如果我们能具体谈谈在JavaScript中,我们能满足.NET分析器的格式。

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

  • <一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString"相对=nofollow> toUTCString()
  • <一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString"相对=nofollow> toISOString()
  • toUTCString()
  • toISOString()

现在我们解析字符串中的服务器端code

Now we parse the string in your server-side code

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

根据你的机器的区域性设置,你可能需要使用 DateTime.ParseExact()而不是 Convert.ToDateTime()

是否有可能通过DateTime类型通过请求?

随着从JavaScript管道输送到你的控制器动作,这将反正转换为字符串或整数。我们可以返回蜱(milisecond)重新presentation的日期时间,但随后你需要将其转换成.NET蜱,它使用一个不同的时代和纳秒为单位。

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.

更多关于解析数据格式<一href="http://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy">here.

More on parsing Date formats here.

这篇关于如何追加日期时间价值FORMDATA和接受它的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆