如何将对象数组从角度前端传递到C#后端

如何将对象数组从角度前端传递到C#后端,c#,arrays,angularjs,asp.net-web-api,controller,C#,Arrays,Angularjs,Asp.net Web Api,Controller,我已经生成了一个角度的对象数组,我希望它将其发送到C#controller。我该怎么做 这是生成对象数组的代码 var addObjectToArray = function (id, price, quantity, tax) { $scope.element = { prodId: id, patientId: $state.params.patientId, clinicId: $cookies.get("clinicId"), user: authSe

我已经生成了一个角度的对象数组,我希望它将其发送到C#controller。我该怎么做

这是生成对象数组的代码

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#控制器中的第二个参数传递

[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);
}
[HttpPost]
[路线(“…路线…”)]
[ResponseType(typeof(int))]
公共IHttpActionResult InsertNewProductTotal(int clinicId)/>
{
var newdactory=_productSaleLogic.InsertNewProductTotal(clinicId,productsList);
已创建的返回(Request.RequestUri.ToString(),newProfession);
}

谢谢你的帮助

假设您的
路线
包含临床医生ID,方式与此类似:

[Route("{clinicId:int}")]
然后,您需要使用正确的类型向控制器操作添加一个参数:

public IHttpActionResult InsertNewProductTotal(int clinicId, [HttpPost] Product[] productsList)
{
    var newAttorney = _productSaleLogic.InsertNewProductTotal(clinicId, productsList);
    return Created(Request.RequestUri.ToString(), newAttorney);
}
其中,
Product
是一个表示javascript对象的类:

public class Product {
    public int prodId {get; set;}
    public int patientId {get; set;}
    //etc.
}
在angular Controller中,您必须使用
$http
服务将对象数组发布到api端点:

$http.post("http://myapihost/myapiPath/" + clinicId, $scope.productsArray)
    .then(function (response) {
        //ok! do something
    }, function (error) {
        //handle error
    });
当然,如果您没有将
clinicId
参数放入
Route
属性中,那么您应该为
$http.post
使用以下URI:
”http://myapihost/myapiPath?clinicId=“+clinicId

创建一个ViewModel(类),其中将包含Id属性和集合(IEnumerable类型)财产
[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
    })