Surya Shrivastava
Surya Shrivastava

Reputation: 25

Processing a POST with a list of JSON in .NET Core API

I am trying to capture user response from a form My Model for the response looks like this:

{
        public int UserId { get; set; }
        public int QId { get; set; }
        public int OptionId { get; set; }
        public string Response { get; set; }

        public SurveyCreatorOptions SurveyCreatorOptions { get; set; }
        public SurveyUserMasters User { get; set; }
    }

The above model and the DbContext was created was created on scaffolding the existing database using efcore database first approach.

The POST method in the controller looks like this:

// POST: api/SurveyUserResponses
        [HttpPost]
        public async Task<IActionResult> PostSurveyUserResponse([FromBody] SurveyUserResponse surveyUserResponse)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.SurveyUserResponse.Add(surveyUserResponse);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (SurveyUserResponseExists(surveyUserResponse.UserId))
                {
                    return new StatusCodeResult(StatusCodes.Status409Conflict);
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetSurveyUserResponse", new { id = surveyUserResponse.UserId }, surveyUserResponse);
        }





Using this method in my controller I can send a post request in the following format:

{
        "userId": 1,
        "qId": 2,
        "optionId": 1,
        "response": "Male",
        "surveyCreatorOptions": null,
        "user": null
    }

The above mentioned method will capture only one field of the form but my form contains multiple fields. So I want to send a JSON of this format

[
{
        "userId": 1,
        "qId": 1,
        "optionId": 1,
        "response": "XYZ",
        "surveyCreatorOptions": null,
        "user": null
    },

{
        "userId": 1,
        "qId": 2,
        "optionId": 1,
        "response": "Male",
        "surveyCreatorOptions": null,
        "user": null
    },

{
        "userId": 1,
        "qId": 3,
        "optionId": 4,
        "response": "Samsung",
        "surveyCreatorOptions": null,
        "user": null
    }
]

I am new to .NET I am confused about how to write the POST method and bind every element of the above list to my database. I have made the Ajax post method which can send a json of above format so that is not a problem

Upvotes: 0

Views: 2044

Answers (1)

itminus
itminus

Reputation: 25350

The above mentioned method will capture only one field of the form but my form contains multiple fields

The builtin ModeBinders will take care that if you change your action to accpet a List<>:

public async Task<IActionResult> PostSurveyUserResponse([FromBody] SurveyUserResponse surveyUserResponse)
public async Task<IActionResult> PostSurveyUserResponse([FromBody] List<SurveyUserResponse> surveyUserResponses)
{
    ....
}

Upvotes: 1

Related Questions