如何从视图日期时间传递asp.net MVC到控制器 [英] How to pass datetime from view to controller in asp.net MVC
问题描述
我试图通过以下数据形成我的观点到控制器。
编辑
<脚本类型=文/ JavaScript的>
VAR路径=的http://+ window.location.host;
VAR学生= [
{名称:维杰,ID:1,出生日期:2010-12-09T08:00:00.000Z},
{名称:阿南,ID:2,DOB:2010-12-09T08:00:00.000Z}
]; $阿贾克斯({
网址:路径+/首页/ UpadetStu
键入:POST,
数据类型:JSON
数据:JSON.stringify(学生)
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(结果){},
故障:功能(R,E,S){警报(E); }
});< / SCRIPT> [ObjectFilter的(参数=stuData,RootType = typeof运算(斯图[]))]
公共JsonResult UpadetStu(斯图[] stuData)
{
返回this.Json(新{成功=真});
}[DataContract]
公共类斯图
{
[数据成员]
公共字符串名称{;组; } [数据成员]
公众诠释ID {搞定;组; } [数据成员]
公众的DateTime? DOB {搞定;组; }}
块引用>但在控制我得到空的名称和标识,DOB默认日期时间,我发现有在通过日期时间的问题。有没有更好的方式来传递从视图日期时间到控制器?我怀念的解析?
解决方案问题是
周四12月9日十三时三十分00秒UTC + 0530 2010
无法解析进入C#中的有效DateTime对象。您可以尝试通过简单地调用DateTime.Parse(星期四12月9日13点30分00秒UTC + 0530 2010)
将失败。我会建议,而不是从服务器返回的日期格式可以更好的回报 ISO 8601 一>格式,看起来像
2010-12-09T08:00:00.000Z
您可以轻松地转换为长日期时间格式为ISO 8601的javascript,
新的日期(星期四12月9日13点30分零零秒UTC + 0530 2010)的toJSON()。
如果您使用的是 JSON.NET 库可以方便地控制方式其中,日期时间都被序列化。
更新:
<脚本类型=文/ JavaScript的> VAR学生= [
{名称:维杰,ID:1,出生日期:2010-12-09T08:00:00.000Z},
{名称:阿南,ID:2,DOB:2010-12-09T08:00:00.000Z}
]; $阿贾克斯({
网址:/首页/索引
键入:POST,
数据类型:JSON
数据:JSON.stringify(学生)
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(结果){},
故障:功能(R,E,S){警报(E); }
});< / SCRIPT>[HttpPost]
公众的ActionResult指数(学生[]学生)
{
...
}Am trying to pass below data form my view to controller.
Edited
<script type="text/javascript"> var pathname = 'http://' + window.location.host; var Student = [ { Name: "Vijay", ID: 1, DOB: "2010-12-09T08:00:00.000Z" }, { Name: "Anand", ID: 2, DOB: "2010-12-09T08:00:00.000Z" } ]; $.ajax({ url: pathname + "/Home/UpadetStu", type: "POST", dataType: "json", data: JSON.stringify(Student), contentType: "application/json; charset=utf-8", success: function (result) { }, failure: function (r, e, s) { alert(e); } }); </script> [ObjectFilter(Param = "stuData", RootType = typeof(Stu[]))] public JsonResult UpadetStu(Stu[] stuData) { return this.Json(new { success = true }); } [DataContract] public class Stu { [DataMember] public string Name { get; set; } [DataMember] public int ID { get; set; } [DataMember] public DateTime? DOB { get; set; } }
But in the controller am getting null for Name and ID , default datetime for DOB, I found that there is problem in passing datetime. Is there any better way to pass datetime from view to controller? do i miss any parsing?
解决方案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 callingDateTime.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:
<script type="text/javascript"> var Student = [ { Name: "Vijay", ID: 1, DOB: "2010-12-09T08:00:00.000Z" }, { Name: "Anand", ID: 2, DOB: "2010-12-09T08:00:00.000Z" } ]; $.ajax({ url: "/Home/Index", type: "POST", dataType: "json", data: JSON.stringify(Student), contentType: "application/json; charset=utf-8", success: function (result) { }, failure: function (r, e, s) { alert(e); } }); </script> [HttpPost] public ActionResult Index(Student[] students) { ... }
这篇关于如何从视图日期时间传递asp.net MVC到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!