0

My ViewModel or Dto is always null at my server side when i send it for example to my Put Method

Here is my client side method

update(company: Company) {  
return this.apiService.put(`${this.path}/${company.id}`, 
company);
}

Put method in apiService

put(path: string, body): Observable<any> {
    debugger;
    this.setBearerHeader();
    console.log('Http Post Observable: ', path, body);
    let url = `${this.baseUrl}${path}`;
    let request = new Request({
        url: url,
        headers: this.headers,
        method: RequestMethod.Put,
        body: body
    });

    return this.http.request(request)
        .catch(this.handleError)
        .map(res => res.json())
};

And my server side model in side which my dto is always null. Is it always required to use [FromBody]?

    [HttpPut("{id}")]
public async Task<IActionResult> Put(string id,ViewModel dto)
{
  if (!ModelState.IsValid)
  {
    return BadRequest(ModelState);
  }

  try
  {
    if (id != dto.Id)
    {
      return BadRequest();
    }
  }
  catch (Exception ex)
  {
    throw;
  }
}
Stefan
  • 1,431
  • 2
  • 17
  • 33

1 Answers1

2

The [FromBody] attribute is required on the dto parameter.

[HttpPut("{id}")]
public async Task<IActionResult> Put(string id, [FromBody]ViewModel dto)
{
    // ...
}
Mathieu Renda
  • 14,069
  • 2
  • 35
  • 33
  • Ok maybe could you help me in my another problem https://stackoverflow.com/questions/45919932/upload-model-with-iformfile-propertis-from-angular2-to-asp-net-core-webapi – Stefan Aug 28 '17 at 16:50
  • And also i want to ask if is there way to not use FromBody? – Stefan Aug 28 '17 at 16:54
  • 1
    Possibly with a custom `IModelMetadataProvider` overriding the `BindingSource`. Do you have any compelling reason to do that? – Mathieu Renda Aug 28 '17 at 17:14
  • I send you a link to my another question and i had hope that it may help. – Stefan Aug 28 '17 at 17:21