【发布时间】:2012-06-25 08:32:18
【问题描述】:
我正在尝试将以下数据从我的视图传递给控制器。
已编辑
<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; }
}
但是在控制器中 Name 和 ID 为空,DOB 的默认日期时间,我发现传递日期时间有问题。有没有更好的方法将日期时间从视图传递到控制器?我错过了任何解析吗?
【问题讨论】:
-
控制器上的 Action 是什么样的?
-
[ObjectFilter(Param = "studentData", RootType = typeof(Stu[]))] public JsonResult UpadetStudent(Stu[] studentData) { return this.Json(new { success = true }); } 我在控制器中使用上面的代码。
-
那么你的 Stu 对象是什么样子的?它有 DOB 属性吗?您是否使用 firebug 或 fiddler 验证了 jQuery.ajax 实际发布到服务器的内容?
-
这就是它的样子。 [DataContract] public class Stu { [DataMember] public string Name { get;放; } [DataMember] 公共 int ID { 获取;放; } [DataMember] 公共日期时间 DOB{ 获取;放; } }
-
我刚刚复制了你的代码并尝试过,它工作得很好。唯一的区别是我没有那个 ObjectFilter。
标签: asp.net-mvc