[英]Parse DateTime? format from ajax to asp.net mvc controller
I would like to parse DateTime?
我想解析
DateTime?
from ajax to asp.net mvc controller. 从ajax到asp.net mvc控制器。
I have following code, where @Model.DateA and @Model.DateB is in DateTime?
我有以下代码,其中@ Model.DateA和@ Model.DateB在
DateTime?
class. 类。 The error i got is uncaught syntax error: unexpected number
我得到的错误是未捕获的语法错误:意外的数字
[HttpPost]
public class CheckDate(DateTime? datea, DateTime? dateb)
{
}
$.ajax({
...
data: {'datea': @Model.DateA, 'dateb': @Model.DateB }
....
});
Use Moment.js. 使用Moment.js。 Moment is prepared to deal with almost every date-format passed to it: ASP.NET JSON Dates, String Dates, UNIX, ISO, etc. In this example i'm using Knockout just to show you the output...
Moment已准备好处理传递给它的几乎所有日期格式:ASP.NET JSON日期,字符串日期,UNIX,ISO等。在此示例中,我使用Knockout只是为了向您显示输出...
//Here you can do this
//var datea = '@Model.DateA.toString()';
//var dateb = '@Model.DateB.toString()';
var datea = "01/23/2015 15:00:00.000";
var dateb = "01/24/2015 15:00:00.000";
//Knockout just to show you date
function MainViewModel(){
var self = this;
//Here i pass to moment.js the date, and format it
//year-month-day; this way is standard for every country
self.datea = ko.observable(moment(datea).format('YYYY-MM-DD'));
self.dateb = ko.observable(moment(dateb).format('YYYY-MM-DD'));
self.sendData = function(){
var request = $.ajax({
url: "/CheckDate", //Your controller method
type: "POST",
data: { datea : self.datea(), dateb: self.dateb() } //Send date values
});
request.done(function(data){
//Do something with the response...
});
};
}
var vm = new MainViewModel();
ko.applyBindings(vm);
For your CheckDate() you are using a class, maybe you want to use JsonResult and send the return with JSON. 对于您的CheckDate(),您正在使用一个类,也许您想使用JsonResult并使用JSON发送返回值。 Something like this:
像这样:
public JsonResult CheckDate(DateTime? datea, DateTime? dateb){
//Check dates
return Json("OK", JsonRequestBehavior.AllowGet);
}
That might solve your problem. 那可能会解决您的问题。 Greetings.
问候。
Ah!... JSBIN working 啊!... JSBIN正在工作
首先将其转换为字符串:
@Model.DateA.Value.ToString("MM-dd-yyyy")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.