2017-06-14 61 views
0

我有看起來像這樣的

public bool Delete([FromBody] List<int> stepIds) 

我已經安裝這樣

api.mySteps = {delete: $resource('my_URL_Here/api/myController?stepIds=:stepIds,{},{delete:{method:'DELETE', hasBody: true}}) }; 
我的$資源DELETE方法的WebAPI控制器傳遞數組

在從UI我的刪除按鈕的點擊,我這樣做:

api.mySteps.delete.delete({'stepIds':[1,2,3]}, 
           function(res){}, 
            function(err){} 
    ); 

在此設置下,我在得到我的NULL WebApi控制器。

在我的瀏覽器控制檯上的網絡選項卡上,我看到:

http://my_URL_Here/api/myController?stepIds=1,2,3

它不傳遞作爲數組。

在小提琴手,如果我做 http://my_URL_Here/api/myController?stepIds=[1,2,3]然後它工作正常。

如何將數組傳遞給DELETE?

回答

0

你需要把它指定到$資源: 對於實例

'delete': {method: 'DELETE', isArray: true} 

,這裏是另一個例子:

var deleteRequest = $resource('/api/delete/post', {}, { 
    'delete': {method: 'DELETE'} 
}); 
deleteRequest.delete({'ids[]':[4,5,6]}); 

那麼你刪除請求URL:

/api/delete/post?ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3 
you get %5B%5D instead of [] 

和如果你期望返回數組而不是對象,那麼你應該使用

'delete': {method: 'DELETE', isArray: true} 

從看你的代碼,你缺少的方括號,所以這應該修復它:

api.mySteps.delete.delete({'stepIds[]':[1,2,3]}, 
           function(res){}, 
            function(err){} 
    ); 
+0

該解決方案是不是爲我工作。我仍然在我的WebApi控制器中獲得NULL。數據需要以這種格式傳遞:'http:// localhost:5903/api/myController?stepIds = [1,2,3]'。用你的方式,數據通過'http:// localhost:5903/api/myController?stepIds [] = 1&stepIds [] = 2'傳遞,這不是控制器所期望的 – nmess88

相關問題