Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of combining your arrays into another array, you're best of sending them as individual parameters to the action method, something like:</p> <p>Assume we still have your two arrays:</p> <pre><code>var features = new Array(); var menuItems = new Array(); menuItems.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); features.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); </code></pre> <p>Then in your JQuery ajax call do the following:</p> <pre><code>$.ajax({ url: '@Url.Action("CheckPreferences")', type: 'POST', datatype: "json", traditional: true, data: { menuItems: JSON.stringify(menuItems), features: JSON.stringify(features) }, success: function () { window.alert('@Resource.AjaxSuccess'); }, error: function (event, request, settings) { window.alert('@Resource.AjaxError' + ' : ' + settings); }, timeout: 20000 }); </code></pre> <p>Then your controller method should be:</p> <pre><code>[HttpPost] public ActionResult CheckPreferences(string menuItems, string features) { var js = new JavaScriptSerializer(); var deserializedMenuItems = (object[])js.DeserializeObject(menuItems); var deserializedFeatures = (object[])js.DeserializeObject(features); var myFeatures = new List&lt;SJSonModel&gt;(); var myMenuItems = new List&lt;SJSonModel&gt;(); if (deserializedFeatures != null) { foreach (Dictionary&lt;string, object&gt; newFeature in deserializedFeatures) { myFeatures.Add(new SJSonModel(newFeature)); } } if (deserializedMenuItems != null) { foreach (Dictionary&lt;string, object&gt; newMenuItem in deserializedMenuItems) { myMenuItems.Add(new SJSonModel(newMenuItem)); } } var myModelList = new SJSonModelList(myFeatures, myMenuItems); return Json(""); </code></pre> <p>I also edited your classes by putting in a constructor to work with the above code, like so: </p> <pre><code>public class SJSonModel { public SJSonModel(Dictionary&lt;string, object&gt; newFeature) { if (newFeature.ContainsKey("Name")) { Name = (string)newFeature["Name"]; } if (newFeature.ContainsKey("isChecked")) { isChecked = bool.Parse((string)newFeature["isChecked"]); } } public string Name { get; set; } public bool isChecked { get; set; } } public class SJSonModelList { public SJSonModelList(List&lt;SJSonModel&gt; features, List&lt;SJSonModel&gt; menuItems ) { Features = features; MenuItems = menuItems; } public List&lt;SJSonModel&gt; Features { get; set; } public List&lt;SJSonModel&gt; MenuItems { get; set; } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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