我想从数据库中删除对象。
我的控制方法:
[HttpPost]
public IActionResult del([FromBody] string p1)
{
try
{
if (p1 != null)
{
RaschetDoppokazR1 obj = db.RaschetDoppokazR1.FirstOrDefault(x => x.P1 == p1);
if (obj != null)
{
db.RaschetDoppokazR1.Remove(obj);
db.SaveChanges();
return Json("OK");
}
else return Json("doesnt exist");
}
else return Json("null");
}
catch (Exception ex)
{
Console.WriteLine(ex);
return Json("Exception");
}
}
和角度查询:
this.http.post('api/data/del', e.data.p1 as string , {responseType: 'json',
headers: new HttpHeaders({
"Content-Type": "application/json"
})
})
.subscribe(data => (console.log(data)));
我有回复:" null"。如果我不注意Content-Type为" application / json",我会收到错误:error
请告诉我,我做错了什么?感谢。
更新:
[HttpPost]
public IActionResult add_RaschetDoppokazR1([FromBody] RaschetDoppokazR1[] obj)
{
try
{
if (obj != null)
{
db.RaschetDoppokazR1.AddRange(obj);
db.SaveChanges();
return Json("ok");
}
else return Json("null");
}
catch (Exception ex)
{
Console.WriteLine(ex);
return Json("exception");
}
}
和角度请求:
this.http.post('api/data/add_RaschetDoppokazR1', JSON.stringify(this.tabl1.filter(x => x.id == null) as RaschetDoppokazR1[]),
{ headers: new HttpHeaders({
"Content-Type": "application/json"
})
})
.subscribe(data => (console.log(data)));
agian我得到空
答案 0 :(得分:0)
您正在将字符串传递给正文,但您说这是“application / json”的内容类型
你需要做的是把json放在体内:
this.http.post('api/data/del', JSON.stringify(e.data.p1 as string) , {responseType: 'json',
headers: new HttpHeaders({
"Content-Type": "application/json"
})
})
.subscribe(data => (console.log(data)));