2016-04-29 18 views
1

オブジェクトの配列をAngularで生成し、C#コントローラに送信します。これどうやってするの?AngularフロントエンドからC#バックエンドへオブジェクトの配列を渡す方法

これは、オブジェクトの配列を生成するためのコードです。

var addObjectToArray = function (id, price, quantity, tax) { 
    $scope.element = { 
    prodId: id, 
    patientId: $state.params.patientId, 
    clinicId: $cookies.get("clinicId"), 
    user: authService.authentication.userName, 
    price: price, 
    quantity: quantity, 
    tax: tax, 
    subtotal: price * quantity, 
    total: price * quantity + tax 
    }; 
    $scope.productsArray.push({ product: $scope.element }); 
} 

これはC#コントローラです。 C#コントローラの2番目のパラメータとしてオブジェクトの配列を渡すにはどうすればよいですか?助けのための

[HttpPost] 
[Route("... the route ...")] 
[ResponseType(typeof(int))] 
public IHttpActionResult InsertNewProductTotal(int clinicId) // << HOW CAN I GET THE ARRAY OF OBJECTS HERE ??? >> 
{ 
    var newAttorney = _productSaleLogic.InsertNewProductTotal(clinicId, productsList); 
    return Created(Request.RequestUri.ToString(), newAttorney); 
} 

ありがとう!

+0

ような何かを行うことができます(タイプのIEnumerableの)Idプロパティとコレクションが含まれますViewModelに(クラス)プロパティを作成します。 – shammelburg

答えて

1

と仮定すると、あなたのRoute含まれているこれと同様の方法でclinicId、:

public IHttpActionResult InsertNewProductTotal(int clinicId, [HttpPost] Product[] productsList) 
{ 
    var newAttorney = _productSaleLogic.InsertNewProductTotal(clinicId, productsList); 
    return Created(Request.RequestUri.ToString(), newAttorney); 
} 

Product

[Route("{clinicId:int}")] 

は、その後、あなたが正しいタイプを使用して、コントローラのアクションにパラメータを追加する必要がありますあなたのjavascriptオブジェクトを表すクラスです:

public class Product { 
    public int prodId {get; set;} 
    public int patientId {get; set;} 
    //etc. 
} 
もちろん

$http.post("http://myapihost/myapiPath/" + clinicId, $scope.productsArray) 
    .then(function (response) { 
     //ok! do something 
    }, function (error) { 
     //handle error 
    }); 

、あなたはRoute属性内clinicIdパラメータを入れていない場合、あなたがすべき:あなたのAPIエンドポイントへのオブジェクトの配列を投稿する$httpサービスを使用する必要があなたの角度のコントローラーで10

$http.postには次のURI:"http://myapihost/myapiPath?clinicId=" + clinicIdを使用してください。

-1
[HttpPost] 
[Route("api/products/{clinicId}")] 

public IHttpActionResult InsertNewProductTotal(int clinicId,[FromBody]Product[]) // << HOW CAN I GET THE ARRAY OF OBJECTS HERE ??? >> 
{ 
    var newAttorney = _productSaleLogic.InsertNewProductTotal(clinicId, productsList); 
    return Created(Request.RequestUri.ToString(), newAttorney); 
} 

はその後角度からは、この

$http.post("http://api/products/" + clinicId, $scope.productsArray) 
    .then(function (response) { 
     //ok! do something 
    }, function (error) { 
     //handle error 
    }) 
関連する問題