2014-02-18 85 views
0

我正在學習angularjsweb.api v2此刻。web.api無法正確綁定JSON對象

我想提交一個簡單的JavaScript對象到web.api 2控制器。
控制器沒有做太多:接收一個對象,並返回它(web.api):

​​

這是用戶類(web.api):

public class User 
{ 
    public int Code { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string Telephone { get; set; } 
    public string Email { get; set; } 
} 

在我的客戶端應用程序已經複製了對象模型。我控制器返回這樣的事情(angularjs):

vm.user = { 
    firstName: '', 
    lastName: '', 
    address: '', 
    city: '', 
    telephone: '', 
    email: '' 
}; 

,這是我的看法:

<div class="list card" ng-controller="userregistration as vm"> 
    <div class="list"> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Nome:</span> 
     <input type="text" placeholder="" ng-model="vm.user.firstName"> 
    </label> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Cognome</span> 
     <input type="text" placeholder="" ng-model="vm.user.lastName"> 
    </label> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Indirizzo</span> 
     <input type="text" placeholder="" ng-model="vm.user.address"> 
    </label> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Città</span> 
     <input type="text" placeholder="" ng-model="vm.user.city"> 
    </label> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Telefono:</span> 
     <input type="text" placeholder="" ng-model="vm.user.telephone"> 
    </label> 
    <label class="item item-input item-stacked-label"> 
     <span class="input-label">Email</span> 
     <input type="text" placeholder="" ng-model="vm.user.email"> 
    </label> 
    </div> 
    <div class="padding"> 
    <button class="button button-block button-positive" ng-click="vm.registerUser(vm.user);">Crea Utente</button> 
    </div> 
</div> 

,這是在我的控制器的registerUser方法(angularjs):

vm.registerUser = function (user) 
{ 
    dataService.registerUser(user) 
    .then(
     function (result) { 
     device.showAlert(result.Code, config.name, 'CONGRATULAZIONI'); 
     }, 
     function (reason) { 
     device.showAlert(reason, config.name, 'ATTENZIONE'); 
     } 
    ); 
} 

我的服務模塊(angularjs)接收用戶對象並將其發佈到我的web.api控制器:

function saveUser(token, user) 
{ 
var deferred = $q.defer(); 

$http({ 
    method: 'POST', url: config.webAPIsBaseUrl + '/api/v1/users', 
    // headers: { 'Authorization': 'Bearer ' + token }, 
    data: { user: user } 
}) 
.success(function (data, status, headers, config) { 
    console.log('saveUser() => success.'); 
    deferred.resolve(data); 
    } 
) 
.error(function (data, status, headers, config) { 
    if (data.Message) { 
     console.log('saveUser() => error 1.'); 
     deferred.reject(data.Message); 
     } 
    else { 
     console.log('saveUser() => error 2.'); 
     deferred.reject("An error occured while registering user!", status); 
     } 
    } 
); 
return (deferred.promise); 
} 

當我將我的user對象發送給我的API時,我的控制器無法正確反序列化用戶。

所有的字段都是空的。

我已經研究了小提琴手一點點,我已經看到該對象被包裹是這樣的:

{"user":{"firstName":"LeftyX","lastName":"","address":"","city":"","telephone":"","email":""}} 

我試圖刪除user包裝:

{"firstName":"LeftyX","lastName":"","address":"","city":"","telephone":"","email":""} 

現在一切按預期工作。

問題:

是否有JavaScript對象到我的控制器模型結合的方法嗎?

回答

2

不是傳遞data: { user: user }$http()的,只是通過user這樣的...

$http({ 
    method: 'POST', url: config.webAPIsBaseUrl + '/api/v1/users', 
    // headers: { 'Authorization': 'Bearer ' + token }, 
    data: user 
}) 
+0

感謝安東尼。有用。乾杯。 – LeftyX