Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can post multiple arrays using JSON.stringify()?
    primarykey
    data
    text
    <p>I am trying to post multiple arrays to my controller using Ajax post. First I have a model like this:</p> <pre><code> public class EnrollmentOptionsVM { public virtual string OptionID{ set;get;} public virtual string UserChoice { set;get;} public virtual string TchOptionID { set; get; } public virtual string TeacherChoice { set; get; } } </code></pre> <p>Then my script:</p> <p></p> <pre><code> &lt;script type="text/javascript"&gt; var $checkboxes = $('input[type="checkbox"]'); var $strInstructors = $('input[name="instructorString"]'); $(document).ready(function () { $('#saveBtn').click(function () { var teacherOptions = []; var options = []; $.each($checkboxes, function () { if ($(this).is(':checked')) { var item = { "UserChoice": "checked", "OptionID": "YouCanSetIDHere" }; } else { var item = { "UserChoice": "unchecked", "OptionID": "YouCanSetIDHere" }; } options.push(item); }) $.each($strInstructors, function () { if ($(this).is(':selected')) { var tchItem = { "TeacherChoice": "checked", "TchOptionID": "SetTchIDHere" }; } else { var tchItem = { "TeacherChoice": "unchecked", "TchOptionID": "SetTchIDHere" }; } options.push(tchItem); }) $.ajax({ type: 'POST', url: '@Url.Action("EnrollmentRefresh", "Student")', contentType: 'application/json', data: JSON.stringify({firstArray:options, secondArray:teacherOptions}) }).done(function (html) { }); }); }); &lt;/script&gt; </code></pre> <p>And in my controller here’s the action signature:</p> <pre><code> [HttpPost] public ActionResult EnrollmentRefresh(List&lt;EnrollmentOptionsVM&gt; checkedOptions) {} </code></pre> <p>When I send only options like this: data: JSON.stringify(options)… it works but when I try to send multiple arrays like the code above it returns null in the controller. How can post multiple arrays using JSON.stringify()?</p> <p>UPDATE 1</p> <p></p> <pre><code> &lt;script type="text/javascript"&gt; var $checkboxes = $('input[type="checkbox"]'); var $strInstructors = $('input[name="instructorString"]'); $(document).ready(function () { $('#saveBtn').click(function () { var teacherOptions = []; var options = []; $.each($checkboxes, function () { if ($(this).is(':checked')) { var item = { "UserChoice": "checked", "OptionID": "YouCanSetIDHere" }; } else { var item = { "UserChoice": "unchecked", "OptionID": "YouCanSetIDHere" }; } options.push(item); }) $.each($strInstructors, function () { if ($(this).is(':selected')) { var tchItem = { "TeacherChoice": "checked", "TchOptionID": "SetTchIDHere" }; } else { var tchItem = { "TeacherChoice": "unchecked", "TchOptionID": "SetTchIDHere" }; } teacherOptions.push(tchItem); }) $.ajax({ type: 'POST', url: '@Url.Action("EnrollmentRefresh", "Student")', contentType: 'application/json', data: JSON.stringify({checkedOptions:options,selectedTeacher:teacherOptions}) }).done(function (html) { }); }); }); &lt;/script&gt; </code></pre> <p>And in the controller:</p> <pre><code> [HttpPost] public ActionResult EnrollmentRefresh( List&lt;EnrollmentOptionsVM&gt; checkedOptions, List&lt;TeacherOptionMV&gt; selectedTeachers) {} </code></pre> <p>Two ViewModels public class TeacherOptionMV {</p> <pre><code> public virtual string TchOptionID { set; get; } public virtual string TeacherChoice { set; get; } } </code></pre> <p>And</p> <pre><code> public class EnrollmentOptionsVM { public virtual string OptionID{ set;get;} public virtual string UserChoice { set;get;} } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload