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

143
Views
Fetch, controller FromBody wrong

Im Fetching to change my Record and the value newInsert is in the fetch true but in the Controller its false.

Fetch:

fetch('api/Test/UpdateOrInsertType', {
            headers: { 'Content-Type': 'application/json' },
            method: 'POST',
            body: JSON.stringify({
                'newInsert': newInsert  //Console.log -> true
            })

Controller:

[HttpPost("UpdateOrInsertType")]
        public IActionResult UpdateOrInsertType([FromBody] bool newInsert) 
// Debugger newInsert -> false
        {
            try
            {
                return Ok(Test.UpdateOrInsertType(newInsert));
            }
            catch (Exception ex)
            {
                return Conflict(ex);
            }
        }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Json doesn't work properly with primitive types. If you wont to use json.stringify you have to create a ViewModel

public class ViewModel
{
public bool NewInsert {get; set;}
}

and action

[HttpPost("UpdateOrInsertType")]
public IActionResult UpdateOrInsertType([FromBody] ViewModel model) 
{
bool newInsert=model.NewInsert

or you can remove { 'Content-Type': 'application/json' }

   fetch('api/Test/UpdateOrInsertType', {
   method: 'POST',
  body: { newInsert: newInsert } 
        })

and remove [FromBody]

[HttpPost("UpdateOrInsertType")]
 public IActionResult UpdateOrInsertType( bool newInsert) 
about 4 years ago · Juan Pablo Isaza Report

0

In this case you should not use JSON.stringify.

Simply pass the raw javascript object to fetch body:

fetch('api/Test/UpdateOrInsertType', {
            method: 'POST',
            body:
            {
                newInsert: newInsert
            }
        });

Now on server side it will be recognized as a bool property.

about 4 years ago · Juan Pablo Isaza 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!