【发布时间】:2014-04-13 23:21:10
【问题描述】:
我按照this 教程创建了一个 Restful web-api 服务。 一切似乎都运行良好,我可以通过从正确的 url 请求以 JSON 格式获取所有预订。 我的问题在于 http POST。
我的 Javascript 是:
var url = 'http://localhost:9077/api/bookings';
....
var newEvent = [];
newEvent.EventDateTime = // (now);
newEvent.Name = "MyFirstBooking";
function btnSubmit_Click()
{
alert("Submit clicked: " + newEvent.Name + "\n" + newEvent.EventDateTime);
$.ajax({
type: "POST",
url: url,
data: JSON.stringify( { Bookings: newEvent }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) { alert(data); }
});
}
警报显示正确的日期和正确的名称。 当我单击提交并检查提琴手时,看起来 JSON 格式正确:
{"Bookings":[{"Name":"MyFirstBooking","EventDateTime":"2014-04-14T13:45:00.000Z"}]}
我的视图是 Bookings.cs :
public class Bookings
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime BookingDateTime { get; set; }
public DateTime EventDateTime { get; set; }
public int Duration { get; set; }
public int UserID { get; set; }
}
在我的 BookingsController 我有:
public HttpResponseMessage PostBooking(Bookings item)
{
// Implementation
}
但是,当我在 PostBooking 之后放置断点时,item.EventDateTime 为 {01/01/0001 00:00:00} 并且 Name 为空。 似乎 JSON 没有被正确反序列化......?我不确定这发生在哪里,因为我在任何地方都找不到提到它...
谢谢。
【问题讨论】:
标签: c# javascript json asp.net-web-api