简体   繁体   English

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

[英]How to pass Array of Objects from Angular Front-end to C# Back-end

I've generated an array of objects in Angular and I want it to send it to the C# controller.我在 Angular 中生成了一个对象数组,我希望它将它发送到 C# 控制器。 How can I do this?我怎样才能做到这一点?

This is the code for generating the array of objects.这是生成对象数组的代码。

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 });
}

This is the C# controller.这是 C# 控制器。 How can I pass the array of objects as the second parameter in the C# Controller?如何将对象数组作为 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);
}

Thanks for help!感谢帮助!

Assuming your Route contains the clinicId, in a way similar to this:假设您的Route包含 ClinicId,方式与此类似:

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

Then you need to add a parameter to your controller action using the correct type:然后,您需要使用正确的类型向控制器操作添加参数:

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

where Product is a class that represents your javascript object:其中Product是代表您的 javascript 对象的类:

public class Product {
    public int prodId {get; set;}
    public int patientId {get; set;}
    //etc.
}

In your angular Controller you have to use the $http service to post the array of objects to your api endpoint:在您的角度控制器中,您必须使用$http服务将对象数组发布到您的 api 端点:

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

Of course, if you are not putting the clinicId parameter inside your Route attribute, then you should use the following URI for your $http.post : "http://myapihost/myapiPath?clinicId=" + clinicId .当然,如果您没有将clinicId参数放在您的Route属性中,那么您应该为您的$http.post使用以下 URI: "http://myapihost/myapiPath?clinicId=" + clinicId

[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);
}

then From angular you can do something like this然后从角度你可以做这样的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不同VS解决方案中的Angular前端和C#后端 - Angular front-end and C# back-end in different VS solution 在c#后端调用api时角度前端获取错误 - Angular front-end getting error when calling api on c# back-end Windows应用商店 - HTML5中的前端和C#/ C ++中的后端 - Windows store applications - Front-end in HTML5 and Back-end in C#/C++ 如何在前端和后端验证必填字段 - How to validate mandatory fields in the front-end and back-end 加载集线器时出错。 HTML5 / js前端和C#后端 - Error loading hubs. HTML5/js front-end and C# back-end 用C#前端和Java后端编程:好的还是坏的做法? - Program with C# front-end and Java back-end: Good or Bad Practice? 如何使用隐藏文件将数据从(前端).aspx 传递到(后端).aspx.cs - How to pass data from (front-end) .aspx to (back-end) .aspx.cs using hidden filed 使用 HTTP Post(RESTful API)将来自 React.JS 前端的请求传递到 C# 后端(ASP.NET) - Passing a Request from React.JS front-end to C# back-end (ASP.NET) using HTTP Post (RESTful API) 保持前端Angular 5和后端Web API代码分离的简单方法? - Simple way to keep Front-end Angular 5 and back-end Web API code separate? 从前端接收 GMT +0 日期字符串时,如何在后端使其成为 GMT +2 日期对象? - When receiving a GMT +0 date string from front-end, how do I make it a GMT +2 Date object in my back-end?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM