【发布时间】:2015-03-08 07:36:39
【问题描述】:
我试图弄清楚如何将对象从我的表单发布到 Web api 服务。在我的控制器中,我定义了一个我想添加输入值的模型。
$scope.Label;
在我的输入字段中,我使用ng-model 绑定它们,例如:
<input type="checkbox" ng-model="label.isPublic" />
<input type="text" ng-model="label.labelName" required focus-me />
在提交表单时,这两个字段a 传递给我的服务并提交给我的WebApi
我已经通过两种方式尝试了这个提交:
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', { params: label }, {
}).then(function (response) {
return response.data;
});
};
还有如下不声明参数
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', label , {
}).then(function (response) {
return response.data;
});
};
在 webAPI 中,我为帖子设置了一个方法
[Route ("reportLibrary/createlabel/")]
[HttpPost]
public DTOs.ReportLabel CreateLabel(DTOs.ReportLabel json)
{
DTOs.ReportLabel result = new DTOs.ReportLabel();
//.... do stuff
return result;
}
ReportLabel (dto) 定义如下:
public class ReportLabel
{
public Int64 LabelId { get; set; }
public string LabelName { get; set; }
public bool IsPublic { get; set; }
public IEnumerable<Report> Reports { get; set; }//placeholder?
}
我遇到的问题是,当我从我的角度服务发布一个对象时,它在 API 中显示为 null。如果我将方法中的类型更改为 JToken 或 JObject 之类的值,则会出现值。
谁能帮我理解为什么当我定义它不是从角度传递的类型时?
谢谢
【问题讨论】:
标签: javascript angularjs asp.net-web-api