【发布时间】:2021-01-19 16:58:49
【问题描述】:
我想将 Angular 中的数据(PostDateTime、Name、Division)作为参数发布到控制器,但我无法传递。
打字稿:
onSubmit(){
this.salesService.postGetData(this.form.value).subscribe(
(res: any) => {
console.log(this.form.value); //I got data here, working fine upto here but cannot post to controller
this.recData = res;
},
err => {
if (err.status == 400)
this.toastr.error('Error !!', 'Authentication failed.');
else
this.toastr.error('Network error', 'Authentication failed.');
//this.loadSpinner.hide();
}
);
Service.ts:
postGetData(data) {
return this.http.post(this.BaseURL + 'Report/FilterCollectionReport', data);
}
控制器:
[Route("FilterCollectionReport")]
[HttpPost]
public IActionResult CollectionReportFilter(string PostDateTime, string Name, string Division) // tried by adding [FormBody] also but does not work
{
string iConn = Configuration["ConnectionStrings:IConnection"];
using (SqlConnection con = new SqlConnection(iConn))
{
List<Object> objList = new List<Object>();
string query = "KK_SP_VIEWSALESENTRY"; // It is SQL procedure takes some parameter/s
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
cmd.Parameters.Add("@PostDateTime", SqlDbType.VarChar).Value = PostDateTime;
cmd.Parameters.Add("@Division", SqlDbType.VarChar).Value = Division;
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
objList.Add(new
{
Id = reader[0].ToString(),
Name = reader[2].ToString(),
Division = reader[3].ToString(), //...etc
});
}
}
con.Close();
}
return Ok(objList);
}
}
当我从 Params 发布数据但不能从 body --> raw --> JSON 发布数据时,它在 Postman 中完美运行
没有类,我正在使用现有数据库..
我有什么遗漏需要添加到我的代码中吗?
【问题讨论】:
标签: asp.net angular asp.net-mvc asp.net-core asp.net-web-api