0

Im POSTing some data via Angular 6, but my Core API keeps returning nulls:

Request:

{"id":0,"name":"test","weight":2,"frequency":2,"activityTypeModelId":3}

Response:

{id: 0, name: null, weight: 0, frequency: 0, activityTypeModelId: 0}

Controller:

[HttpPost("[action]")]
public IActionResult Add([FromForm]Model model)
{
    return new JsonResult(model);
}

Angular, using HttpClient:

add(Model: model) {
     return this.http.post(this.addUrl, model);
}

API Model:

public class Model
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int Weight { get; set; }
    public int Frequency { get; set; }
    public int ActivityTypeModelId { get; set; }
}

TS Model:

 export class Model{
   id?: number;
   name?: string;
   weight?: number;
   frequency?: number;
   activityTypeModelId?: number;
 }

Everything works fine when I'm using Postman. I already tried with [FromBody]. Where is the problem?

8
  • 1
    Your action is returning something named newCircumstance, what is that? Commented Aug 30, 2018 at 18:34
  • What do you see in the network tab of developer console in your browser? Are you able to see request body over there? Commented Aug 30, 2018 at 18:41
  • When you break in the C# Action is Model populated? When you look at Browser Dev tools what is getting submitted? Commented Aug 30, 2018 at 18:41
  • @R.Richards , i forgot to change that name here after pasting. I changed it in the edit. Commented Aug 30, 2018 at 19:14
  • @AmitChigadani No, there is no requestbody. Commented Aug 30, 2018 at 19:14

4 Answers 4

3

I dont know why, but this fixed my issue:

I created a header:

 const header = new HttpHeaders()
     .set('Content-type', 'application/json');

Changed the POST function by adding a header and JSON.Stringyfy the object:

 add(model: Model): Observable<Model> {
     const body = JSON.stringify(c);
     return this.http.post<Model>(this.addUrl, body, { headers: header} );
   }

Changed [FromForm] to [FromBody].

Adding JSON.stringify(model) in the parameters of the http.post was not working.

JSON that is working with the CORE Api:

{"name":"test","weight":2,"activityTypeModelId":15}

JSON that is not working with the CORE Api:

{name:"test",weight:2,activityTypeModelId:15}

Without the header I encountered a 415 error from the API.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

return this.http.post(this.addUrl, JSON.stringify(model) );

Comments

0

I think that, in .NET Core 2.1 is (see https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1)

HttpPost("[action]")]
//see that I put [FromBody]
public IActionResult Add([FromBody]Model model)
{
    //OK is one of several IActionResult 
    return OK(model);
}

Comments

0

I had this issue and the problem was that I was trying to bind to an interface:

[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]ISaveRequestViewModel request) =>
    Service.SaveResponse(request.Responses);

I fixed it by changing it to a class:

[HttpPost("[action]")]
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK]
public bool SaveResponse([FromBody]SaveRequestViewModel request) =>
    Service.SaveResponse(request.Responses);

This model also had to use classes instead of interfaces:

public class SaveRequestViewModel
{
    public List<IQuestionResponseViewModel> Responses { get; set; }
}

Became

public class SaveRequestViewModel
{
    public List<QuestionResponseViewModel> Responses { get; set; }
}

I guess the model binder just doesn't work with interfaces.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.