2016-06-30 88 views
0

所以,我有一個angularJS數組,我想將它傳遞給ASP.Net MVC方法,然後將其數據存儲在數據庫中。傳遞angularjs數組到ASP.Net MVC方法

數組如下所示:

telephone = [{'id':'T1', 'contactN':'212-289-3824'}, {'id':'T2', 'contactN':'212-465-1290'}];

當我點擊一個按鈕,它觸發以下JS功能:

$scope.updateUserContacts = function() { 
    $http.post('/Home/UpdateUserContacts', { contactsData: $scope.telephone }) 
     .then(function (response) { 
      $scope.users = response.data; 
     }) 
    .catch(function (e) { 
     console.log("error", e); 
     throw e; 
    }) 
    .finally(function() { 
     console.log("This finally block"); 
    }); 
} 

我的問題是,我怎麼能接受這個數組在我的ASP.Net MVC?什麼格式可以與這個數組兼容?

下面是一個ASP.Net MVC方法的例子,但我不知道什麼類型和/或如何接收傳遞的數組?

[HttpPost] //it means this method will only be activated in the post event 
    public JsonResult UpdateUserContacts(??? the received array) 
    { 
     ...... 
} 

回答

1

類型應該是ListArray

[HttpPost] //it means this method will only be activated in the post event 
    public JsonResult UpdateUserContacts(List<MyObj> contactsData) 
    { 
     ...... 
    } 

OR

public JsonResult UpdateUserContacts(MyObj[] contactsData) 
    { 
     ...... 
    } 

你應該有這樣

public class MyObj 
{ 
    public string id {get;set;} 
    public string contactN {get;set;} 
} 
2

模型類在你的MVC應用A你應該有電話分類

class Telephone 
{ 
    public string id; 
    public string contactN; 
} 

[HttpPost] //it means this method will only be activated in the post event 
public JsonResult UpdateUserContacts(Telephone[] contactsData) 
{ 
     //Do something... 
}