[英]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.