0

I am using javascript code to postdata to a controller action coded in C#. I am getting a null value when attempting to pass an array of integers in javascript created as follows:

var answers = [];
answers.push(1);
answers.push(2);
answers.push(3);

I can see from javascript alert that the values are going into array as when I try answers.tostring I am getting the value "1,2,3" as expected, I am then trying the following postdata

var postData = {
                'surveyID': surveyID,
                'questionID': questionID,
                'answers': answers,
                'answerText': null,
                'comment': comment,
                'questionType': questionType
            };

This is targetting the following controller action but the array of integers does not seem to be carrying across to the controller action as my List is always null

[HttpPost]
public PartialViewResult GoForward(int surveyID, int questionID, List<int> answers, string answerText, string comment, string questionType)
{}

Any know how I can get the postdata array to translate to a List that is not null?

2
  • Yes my question says that, I am looking for a means of passing it this way Commented Apr 23, 2014 at 0:32
  • 1
    Why cannot use Array class in C#? Commented Apr 23, 2014 at 0:44

1 Answer 1

0

Because MVC expects the data in QueryString pattern like this:
answers=1&answers=2&answers=3

If you use jQuery to do AJAX, default is
answers[]=1&answers[]=2&answers[]=3

You could try traditional option

$.ajax({
  //...
  traditional:true,
  //...
});

refer to https://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

Comments

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.