我使用c#和MV在MVC5中工作 我将数据发布到JSON中的Web-api2服务器,如下所示:
function SendSms() {
var studentData = {
Id: "4",
ClientId: "2",
NoOfRecipient: "3",
Msg: "This is a test",
Numbers: [
{ Number: "988888888", Id: "3"},
{ Number: "988888787", Id: "5"}
]
};
console.log(JSON.stringify(studentData));
$.ajax({
cache: false,
type: "POST",
url: "http://localhost/ProductsApp/api/SmsClients",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(studentData),
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
console.log(data);
},statusCode: {
201 /*Created*/: function (data) {
console.log("created");
}
}
})
.fail(
function (xhr, textStatus, err) {
console.log("Error");
});
}
并接受像这样的JSON:
public HttpResponseMessage Post(SmsData smsData)
{
//code to be performed after getting Post
}
我的SmsData模型是这样的:
public class SmsData
{
public int Id { get; set; }
public int ClientId { get; set; }
public int NoOfRecipient { get; set; }
public List<Numbers> Numbers { get; set; }
public string Msg { get; set; }
public DateTime SentDate { get; set; }
}
public class Numbers{
public int Id { get; set; }
public long Number { get; set; }
}
但是我得到了smsData Null的Numbers属性。为什么会这样......为什么Numbers属性为空。我怎样才能获得数字
答案 0 :(得分:0)
使用数组而不是列表应该会有所帮助,但您也可以尝试在执行ajax调用时设置“traditional:true”参数。
$。AJAX({ cache:false, 传统:真实, 键入:“POST”,