Has*_*him 2 c# jquery asp.net-mvc-3
可能重复:
DateTime.ParseExact格式字符串
如何将字符串转换为DateTime对象?
例:
Sun Oct 07 2012 00:00:00 GMT + 0500(巴基斯坦标准时间)
我试过,DateTime.Parse,Convert.TODateTime等无效.我收到一个错误,它不是一个有效的DateTime字符串.
以下是我如何从jquery向MVC控制器的action方法发送datetime:
$.ajax({
url: '@Url.Action("actionMethodName", "controllerName")',
type: "GET",
cache: false,
data: {
startDate: start.toLocaleString(),
endDate: end.toLocaleString()
},
success: function (data) {
}
});
Run Code Online (Sandbox Code Playgroud)
我需要能够在控制器操作方法中获取日期时间:
public JsonResult actionMethodName(string startDate, string endDate)
{
if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
{
var start = DateTime.Parse(startDate); //Get exception here
var end = DateTime.Parse(endDate); //Get exception here
}
//Rest of the code
}
Run Code Online (Sandbox Code Playgroud)
我建议你.toJSON()在你的javascript Date实例上使用这个方法,以便将它们序列化为ISO 8601格式:
$.ajax({
url: '@Url.Action("actionMethodName", "controllerName")',
type: "GET",
cache: false,
data: {
startDate: start.toJSON(),
endDate: end.toJSON()
},
success: function (data) {
}
});
Run Code Online (Sandbox Code Playgroud)
现在您不需要解析控制器中的任何内容,您将直接使用日期:
public ActionResult ActionMethodName(DateTime startDate, DateTime endDate)
{
//Rest of the code
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6488 次 |
| 最近记录: |