0

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?

6
  • can you post the code how you are building the choices objects? From your screenshot it seems that OptionId contains function rather than string Commented Dec 23, 2017 at 12:08
  • I'm using knockoutJs. Even if OptionId is a function, shouldn't it map OptionName property? Commented Dec 23, 2017 at 12:13
  • Actualy, you are trying to send array of javascript objects, i don't think your backend will be able to process that anyway. Try to send it as JSON.stringify(choices) and parse it on your backend side Commented Dec 23, 2017 at 12:19
  • when I send JSON.stringify(choices) , it doesn't even receive the count in List<QuestionOption> choices parameter. let me try dynamic JObject.Parse(choice); Commented Dec 23, 2017 at 12:21
  • Yes, because you are getting a string representation of your Object Array. In your original code, you are receiving count, because you are sending array of objects, but all of them are undefined, as you can't post an array of javascript objects. Commented Dec 23, 2017 at 12:24

1 Answer 1

1

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>));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks mike, it worked! I should post the question earlier, I wasted my whole day.
Btw. if you don't mind, post the line you use to parse it on your backend, so i can update the asp.net code in the answer to a working solution, i was just blind guessing the asp code
sure, I parsed using List<QuestionOption> choices = (List<QuestionOption>) JsonConvert.DeserializeObject(choicesJson, typeof(List<QuestionOption>));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.