0

I have a JS Array of Objects which, at time of Post contains three variables per object:

ParticipantId,
Answer,
ScenarioId

During post, there is an Array the size of 8 (at current anyway) which all correctly contain data. When I call post request, the Controller does get hit as the breakpoint triggers, the issue is when I view the List<SurveyResponse> participantScenarios it is shown as having 0 values.

The thing I always struggle to understand is that magic communication and transform between JS and .Net so I am struggling to see where it is going wrong.

My JS Call:

postResponse: function () {
            var data = JSON.stringify({ participantScenarios: this.scenarioResponses})
            // POST /someUrl
            this.$http.post('ScenariosVue/PostScenarioChoices', data).then(response => {
                // success callback
            }, response => {
                // error callback
            });
        }

My .Net Core Controller

[HttpPost("PostScenarioChoices")]
        public async Task<ActionResult> PostScenarioChoices(List<SurveyResponse> participantScenarios)
        {
            List<ParticipantScenarios> addParticipantScenarios = new List<ParticipantScenarios>();

            foreach(var result in participantScenarios)
            {
                bool temp = false;
                if(result.Answer == 1)
                {
                    temp = true;
                }
                else if (result.Answer == 0)
                {
                    temp = false;
                }
                else
                {
                    return StatusCode(400);
                }

                addParticipantScenarios.Add(new ParticipantScenarios
                {
                    ParticipantId = result.ParticipantId,
                    Answer = temp,
                    ScenarioId = result.ScenarioId
                });
            }
            try
            {
                await _context.ParticipantScenarios.AddRangeAsync(addParticipantScenarios);
                await _context.SaveChangesAsync();
                return StatusCode(201);
            }
            catch
            {
                return StatusCode(400);
            }
        }
UncountedBrute
  • 484
  • 1
  • 7
  • 25
  • Just for kicks, can you try not stringifying in the request? Like: `var data = { participantScenarios: this.scenarioResponses}; // removed stringify` – acdcjunior Mar 31 '18 at 16:03
  • Could you open the network tab in the chrome devtools while sending the data to verify you're not actually sending empty data? – Philip Feldmann Mar 31 '18 at 16:07

0 Answers0