Using Angular 6 & C# here.
On the button click event of my form I want to save the data. The event calls an API which saves the data.
Below is my button click event:
submitData() {
//After getting form values I create an object as below
let myData =
{
"name":name,
"date": date,
"status": status,
"selection":selection,
"createdBy": createdBy
};
this.myService.save(myData)
.subscribe(
(jsonData) => {
//redirect etc after save
});
}
In my above event selection itself holds array of objects as:
0: {name: "u1", id: "123", type:"dev", comment: "comment1"}
1: {name: "u2", id: "456", type:"prd", comment: "comment2"}
Also below is my service call:
saveExtract(data) {
return this.httpClient.post<any>(this.url + '/Save', data)
.pipe(
catchError())
);
}
Finally below is my c# api;
[HttpPost("Save", Name = "Save")]
public async Task Save([FromBody] SaveModel data)
{
//do save here etc
}
But above code returns me 400 (Bad Request) error.
I even try to put the breakpoint in my api but its never hit.
So looks like something is wrong while I call my post.
Can Anyone point out the issue?
Thanks
---Updated---
So I was missed out the code to pass httpOptions and using Json.stringify. After passing it partially works as below:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
saveExtract(data) {
return this.httpClient.post<any>(this.url + '/Save', data, httpOptions)
.pipe(
catchError())
);
this.myService.save(JSON.stringify(myData))
.subscribe(
(jsonData) => {
//redirect etc after save
});
The above code works when I comment out the selection array which is a part of my data to be passed to the API. But does not work and gives bad request error when I pass selection array.
So can anyone point how can I pass array of object to it.
SaveModel? Do you have any server side Action Filters on yourSaveaction or global Action Filters?SaveModel? Share us the full controller code.