angularjs发送嵌套的对象到API
问题描述:
您好我正在开发与Web API应用程序的角度。我试图从角度接收数组的对象。不幸的是我在阵列中收到null
。angularjs发送嵌套的对象到API
this.updateprocesswithtemplates = function() {
var sub = {
projectId: "50",
code: "app",
templates: {
title: "templatetiltle",
version: "templateversion",
visible: "templatevisible",
templatefileid: "31",
displayorder: "1"
}
};
var responseservice = $http.put('/api/processes/81/', sub).success(function (response) {});
return responseservice;
}
在网络API我具有低于类
public class updatercvparams
{
public string projectId{ get; set; }
public int code { get; set; }
public templates[] templates { get; set; }
}
public class templates
{
public string title { get; set; }
public string version { get; set; }
public int displayorder { get; set; }
public string visibility { get; set; }
public int templatefileid { get; set; }
}
当我发送上述请求对象,我将在模板接收null
。
public HttpResponseMessage Put(int id, updatercvparams obj)
我可以知道,我在这里丢失了什么吗?
答
由于您已将templates
定义为数组,因此您需要发送数组而不是对象。
var sub = {
projectId: "50",
code: "app",
templates: [{
....
}]
};
答
根据web api中的实体(类),字段名称在JavaScript对象中应该是相同的。当您发送像数组一样的数据列表时,请使用方形苞片('[]')。
this.updateprocesswithtemplates = function() {
var sub = {
projectId: "50",
code: "app",
templates: [{
title: "templatetiltle",
version: "templateversion",
visibility: "templatevisible",
templatefileid: "31",
displayorder: "1"
}]
};
var responseservice = $http.put('/api/processes/81/', JSON.stringify(sub))
.success(function(response) {});
return responseservice;
}
感谢。有效... –