Reputation:
I've faced with problem. When I'm trying to post object to Web Api it comes as null. But if I use object like this
let obj = { "name":"name", "phone":"phone"} -
webapi controller get it and process correctly.
my ts class
export class Preorder {
public vincode: string;
public phone: string;
public name: string;
public id: number;
constructor(id, vincode, phone, name) {
this.id = id;
this.vincode = vincode;
this.phone = phone;
this.name = name;
}
service method that sends info
public addPreorder(preorder: Preorder){
let api_dest = "/api/preorder/post";
let result;
let objectToSend = JSON.stringify(preorder);
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post(this.test_url + api_dest, objectToSend , { headers: headers })
.map((res: Response) => res.json()).subscribe(res => {
result = res;
console.log(result);
});
}
api method
[HttpPost]
[Route("api/preorder/post")]
public IActionResult Post([FromBody]PreorderApi preorder)
{}
PreordreApi.cs
[Serializable]
public class PreorderApi
{
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public string Phone {get;set;}
[JsonProperty]
public string VinCode { get; set; }
[JsonProperty]
public int Id { get; set; }
}
I've added [Serializable] and [JsonProperty] for model but it doesn't help. There a lot of examples how I can post data , I've tried few of them but didn't get result. I understand that something wrong with my ts class , but can't figure what.
Upvotes: 0
Views: 88
Reputation:
My fault, I've missed fill id in param of Preorder.ts. But I can't get why id has that effect.
Upvotes: 0
Reputation: 1096
Try to add property names:
public class PreorderApi
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("phone")]
public string Phone {get;set;}
[JsonProperty("vincode")]
public string VinCode { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
}
Upvotes: 0