Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
ASP.NET Core MVC controller not binding

Doing a request from the fetch api to the endpoint and the props do not bind.

The id and fileName are 0 and null respectively on the endpoint.

My fetch:

fetch(`https://localhost:44343/items/edit`, {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                id:123,
                fileName:"asd"
            })
        })
            .then(response => console.log(response))
            .catch(error => console.error('Unable to update item.', error));

My endpoint:

[HttpPost]
public async Task<IActionResult> Edit([FromBody]int id, string fileName)
{
    return Ok(id);
}

The request payload shows that the values are being sent:

network request

I've tried using and not using the [FromBody] adding explicitly the route to the action, changing to PUT instead of POST (why the heck does the default for the update action is a POST??)

Anything else I can try?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Create a class which will represent the json structure:

public class Request
{
   public int id {get; set;} 
   string fileName {get; set;} 
}

And accept it in the action:

public async Task<IActionResult> Edit(Request r)
{
   // use r
}

More on model binding in ASP.NET Core.

over 4 years ago · Santiago Trujillo Report

0

As stated in previous comment, create an object model to represent the json

public class Request
{
   public int Id {get;set;}
   public string FileName {get;set;}
}

At your controller action method

   [HttpPost]
   [Route("items/edit")]
   public IActionResult Edit([FromBody] Request req)
   {
     return Ok();
   }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!