Javascript 以角度传递要发布的对象数组
我试图在Angular controller中创建一个函数,该函数将Javascript 以角度传递要发布的对象数组,javascript,c#,angularjs,arrays,json,Javascript,C#,Angularjs,Arrays,Json,我试图在Angular controller中创建一个函数,该函数将TestExpense对象数组传递给C#API,然后C#API将处理将它们插入数据库的操作。目前,API配置为一次只执行一个操作 我希望传递的数组对象,$scope.TestExpenses,由以下构造函数表示: function TestExpense(employeeId, expenseDate, taskId, expenseTypeId, billingCategory, notes, amount, last
TestExpense
对象数组传递给C#API,然后C#API将处理将它们插入数据库的操作。目前,API配置为一次只执行一个操作
我希望传递的数组对象,$scope.TestExpenses
,由以下构造函数表示:
function TestExpense(employeeId, expenseDate, taskId, expenseTypeId,
billingCategory, notes, amount, lastUpdatedDate, lastUpdatedBy, dateSubmitted) {
this.employeeId = employeeId;
this.expenseDate = expenseDate;
this.taskId = taskId;
this.expenseTypeId = expenseTypeId;
this.billingCategory = billingCategory;
this.notes = notes;
this.amount = amount;
this.lastUpdatedDate = lastUpdatedDate;
this.lastUpdatedBy = lastUpdatedBy;
this.dateSubmitted = dateSubmitted;
this.location = location;
}
$scope.TestExpenses = [];
相关角度函数的当前状态<代码>Submittenties数据库:
$scope.submitEntriesToDatabase = function () {
$http.post('/expense/submitExpense', $scope.TestExpenses)
.success(function (data, status, headers, config) {
})
.error(function (data, status, headers, config) {
});
};
在C#中,我的TestExpense
对象对应以下模型:
public class Expense
{
public int employeeId { get; set; }
public string expenseDate { get; set; }
public int taskId { get; set; }
public int expenseTypeId { get; set; }
public int billingCategory { get; set; }
public string notes { get; set; }
public float amount { get; set; }
public string LastUpdatedDate { get; set; }
public int LastUpdatedBy { get; set; }
public string dateSubmitted { get; set; }
public string location { get; set; }
}
以及在API中处理POST
的方法:
public void SubmitExpenses(List<Expense> expenses)
{
//using(cnxn)
//{
// using(SqlCommand sqlQuery = new SqlCommand("INSERT INTO Expenses " +
// "(Employee_ID, Task_ID, Expense_Date, Expense_Type_ID, Billing_Category_ID, " +
// "Amount, Notes, Last_Updated_By, Last_Update_Datetime, Date_Submitted, Location) " +
// "Values (@employeeId, @taskId, @expenseDate, @expenseTypeId, @billingCategory, @amount, @notes, " +
// "@lastUpdatedBy, @lastUpdatedDate, @dateSubmitted, @locationId)", cnxn))
// {
// sqlQuery.Parameters.Add(new SqlParameter("@employeeId", SqlDbType.Int) { Value = employeeId });
// sqlQuery.Parameters.Add(new SqlParameter("@expenseDate", SqlDbType.DateTime) { Value = expenseDate });
// sqlQuery.Parameters.Add(new SqlParameter("@taskId", SqlDbType.Int) { Value = taskId });
// sqlQuery.Parameters.Add(new SqlParameter("@expenseTypeId", SqlDbType.Int) { Value = expenseTypeId });
// sqlQuery.Parameters.Add(new SqlParameter("@billingCategory", SqlDbType.Int) { Value = billingCategory });
// sqlQuery.Parameters.Add(new SqlParameter("@notes", SqlDbType.Text) { Value = notes });
// sqlQuery.Parameters.Add(new SqlParameter("@amount", SqlDbType.Money) { Value = amount });
// sqlQuery.Parameters.Add(new SqlParameter("@lastUpdatedDate", SqlDbType.DateTime) { Value = lastUpdatedDate });
// sqlQuery.Parameters.Add(new SqlParameter("@lastUpdatedBy", SqlDbType.Int) { Value = lastUpdatedBy });
// sqlQuery.Parameters.Add(new SqlParameter("@dateSubmitted", SqlDbType.DateTime) { Value = dateSubmitted });
// sqlQuery.Parameters.Add(new SqlParameter("@locationId", SqlDbType.VarChar) { Value = "Radnor" });
// cnxn.Open();
// sqlQuery.ExecuteNonQuery();
// }
//}
}
任何指导都将不胜感激
根据我收集的信息,我需要反序列化表示$scope.TestExpenses数组的JSON字符串,但我不确定如何开始从中解析出各个费用对象
简单。在方法签名中使用[FromBody]
装饰符。您为所有控制器设置的默认JSON序列化程序设置将尝试将字符串解析到费用
列表中-如果您使用的是JSON.NET,它可以自动处理此问题
public void SubmitExpenses([FromBody] List<Expense> expenses)
public void submittexpenses([FromBody]列出费用)
根据我收集的信息,我需要反序列化表示$scope.TestExpenses数组的JSON字符串,但我不确定如何开始从中解析出各个费用对象
简单。在方法签名中使用[FromBody]
装饰符。您为所有控制器设置的默认JSON序列化程序设置将尝试将字符串解析到费用
列表中-如果您使用的是JSON.NET,它可以自动处理此问题
public void SubmitExpenses([FromBody] List<Expense> expenses)
public void submittexpenses([FromBody]列出费用)
根据我收集的信息,我需要反序列化将
表示我的$scope.TestExpenses数组,但我不确定如何
开始从中解析出各个费用对象
否。您不需要做任何事情来反序列化。它将自动反序列化
更好地实现POST请求将是这样的
$scope.submitEntriesToDatabase = function() {
var config = {
method: "POST",
url: '/expense/submitExpense',
data: $scope.TestExpenses
};
$http(config).then(function(response) {
//success
}, function(response) {
//error
});
};
若数组中的属性名和泛型列表中对象的属性名相同,web api将自动将javascript数组转换为list
注意:不要使用.success
和.error
回调,因为它们已过时。使用。然后
根据我收集的信息,我需要反序列化将
表示我的$scope.TestExpenses数组,但我不确定如何
开始从中解析出各个费用对象
否。您不需要做任何事情来反序列化。它将自动反序列化
更好地实现POST请求将是这样的
$scope.submitEntriesToDatabase = function() {
var config = {
method: "POST",
url: '/expense/submitExpense',
data: $scope.TestExpenses
};
$http(config).then(function(response) {
//success
}, function(response) {
//error
});
};
若数组中的属性名和泛型列表中对象的属性名相同,web api将自动将javascript数组转换为list
注意:不要使用
.success
和.error
回调,因为它们已过时。使用。然后使用$http在angular 1.x中执行post的正确方法是:
$http.post('/my/url',{foo:bar}).then(function(result) {
console.log(result.data); // data property on result contains your data
});
在angular 1.x中使用$http进行post的正确方法是:
$http.post('/my/url',{foo:bar}).then(function(result) {
console.log(result.data); // data property on result contains your data
});
设置的路由是什么,如何应用于此功能?如$http.post
中所示。我知道数据被传递到我的控制器中的submitPenses
方法,但我不知道如何分解作为参数传递的List
,我觉得这是一个独立于路由的问题。我编辑了这篇文章,因为我没有正确缩进SubmitClasses
方法头。抱歉。请澄清如何在您的服务器上调用SubmitExpenses
函数。该路由已发布,但我不太清楚它的相关性,因为我知道应用程序和API已连接。实际上,只更改了方法头,这与路由无关。设置的路由是什么,如何将其应用于此函数?如$http.post
中所示。我知道数据被传递到我的控制器中的submitPenses
方法,但我不知道如何分解作为参数传递的List
,我觉得这是一个独立于路由的问题。我编辑了这篇文章,因为我没有正确缩进SubmitClasses
方法头。抱歉。请澄清如何在您的服务器上调用SubmitExpenses
函数。该路由已发布,但我不太清楚它的相关性,因为我知道应用程序和API已连接。从字面上看,只有方法头被更改,这与路由无关。我现在就试试这个。因此,我应该能够从expenses
参数中通过索引将元素作为Expense
对象进行访问,然后访问每个元素的属性?+1以获得帮助,@toadflakz。实际上,我并不需要@naveen详细说明的[FromBody]
,但我很感激你帮助我走上了正确的道路。他是正确的。只是web api使用JSON.NET+1I,我现在就尝试一下。因此,我应该能够从expenses
参数中通过索引将元素作为Expense
对象进行访问,然后访问每个元素的属性?+1以获得帮助,@toadflakz。实际上,我并不需要@naveen详细说明的[FromBody]
,但我很感激你帮助我走上了正确的道路。他是正确的。只是web api使用JSON.NET+1非常感谢!另外,+1让我深入了解了$http.post
$http.post
的正确用法,这同样很好$http
为我们提供了更多的控制。在我看来,它也更具可读性,非常感谢你!另外,+1让我深入了解了$http.post
$http.post
的正确用法,这同样很好$http
为我们提供了更多的控制。它的