Irfan Y
Irfan Y

Reputation: 1300

unable to get array from ajax to controller

I have an array of objects that I'm passing from view to controller using ajax. Array data is: enter image description here

My ActionMethod is:

public JsonResult AddQuestionsToStep(long stepId, string questionText, string questionType, string correctAnswer = "", List<QuestionOption> choices = null)

I'm receiving other variables data and count for choices array but the data in choices array is not mapped.i.e, OptionName that has value on client side is null on server side. What i'm doing wrong?

Upvotes: 0

Views: 597

Answers (1)

mike_t
mike_t

Reputation: 2691

As your choices is a javascript array of Objects, serialize it and parse it on your backend:

choices: JSON.stringify(choices)

On your backend just parse the json:

List<QuestionOption> choices = (List<QuestionOption>) JsonConvert.DeserializeObject(choicesJson, typeof(List<QuestionOption>));

Upvotes: 1

Related Questions