1
votes

I have a function to call API like this

async addOffDayReplacement(employeeId: string, days: number) {
    console.log(days);
    await Axios.post('/api/v1/leaveQuota/' + employeeId, {
        params: {
            additionalDays: days
        }
    });
}

And have API

[HttpPost("{id}")]
public async Task<ActionResult<bool>> Update(string id, int additionalDays)
{
    await leaveService.AddReplacement(id, additionalDays);
    return true;
}

But the additionalDays in my API always 0 even when value that I sent from my service is not 0

Anybody know why? And how to solve this?

2

2 Answers

1
votes

Use an object as your second parameter which contains AdditionalDays property. Annotate that parameter with [FromBody].

[HttpPost("{id}")]
public async Task<ActionResult<bool>> Update(string id, [FromBody] Model model)
{
    await leaveService.AddReplacement(id, model.AdditionalDays);
    return true;
}

Do not forget to define a Model class with AdditionalDays public property.

For details, take a look at this article: https://weblog.west-wind.com/posts/2012/Sep/11/Passing-multiple-simple-POST-Values-to-ASPNET-Web-API

1
votes

You need to specify the binding source, i.e. how your function can access it/how the parameter is passed (body, query, etc.), so using

[FromBody]int additionalDays

should fix it for you.

The reason why string id works is because you specified it in [HttpPost("{id}")].

I'd recommend checking out https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1 for more details and examples.