【发布时间】:2015-01-25 15:03:36
【问题描述】:
我有一个 web api 2.0 应用程序,我必须发布表单的数据。这里是我的 js 代码:
(function (angular) {
var public_area = angular.module("public-area", ['ngResource']);
public_area.factory("MyService", function ($resource) {
return {
my_controller: $resource("/api/MyHttpControllerTest/")
};
});
public_area.controller("MyController", function ($scope, MyService) {
$scope.getPlans = function ()
{
var pianiTariffari =
new MyService
.my_controller
.query({
customerType: $scope.customerType
}, isArray = true);
}
$scope.sendInvitation = function ( )
{
var invitation =
new MyService
.my_controller(
{
request: {
CustomerType: "test"
}
});
invitation.$save();
}
});
})(angular);
你怎么看,我有一个控制器..和两个函数,1个在GET和1个在POST......
我看到你也是服务器端控制器
public FillFormPresentaUnAmicoResponse GetPromozioni(string customerType)
{
...
}
[HttpPost]
public PresentaUnAmicoResponse InvitaAmico(PresentaUnAmicoRequest request)
{
...
}
PresetaUnAmicoRequest 类在哪里
public class PresentaUnAmicoRequest
{
public string CustomerType { get; set; }
}
GET 功能正常工作!但是POST不是... 在帖子中,我收到了“请求”的有效实例,但其中的属性为 NULL...
对我来说真正奇怪的是,如果我以这种方式调用 Post 方法:
var invitation = new MyService.my_controller(undefined);
invitation.$save();
服务器端,“请求”是实例化的。我期望“请求 = null”代替..
谁能帮帮我? 谢谢
更新:
如果我理解的话,我总是需要同一个对象来坚持 REST 原则。所以,根据spike的例子,我相信我必须创建一个类似的对象:
public class Promos
{
public string CustomerType{ get; set; }
public List<string> PromoPerCustomerType{ get; set; }
}
public class PresentaAmico
{
public Promos Promozioni { get; set; }
public string Name{ get; set; }
public string Surname { get; set; }
...
}
而我的 Rest 服务应该是这样的
[HttpGet]
public PresentaAmico Get(string customerType)
{
...
}
[HttpPost]
public void Save(PresentaAmico myForm)
{
...
}
正确吗?
【问题讨论】:
标签: angularjs resources http-post asp.net-web-api2