Note that there are some explanatory texts on larger screens.

plurals
  1. POSending an array of json objects to action with ajax in asp.net mvc 3
    primarykey
    data
    text
    <p>I hope anyone can help me (Sorry for my english). I have a problem when I want to send un array of arrays in ajax. My model is:</p> <pre><code>public class SJSonModel { public string Name { get; set; } public bool isChecked { get; set; } } public class SJSonModelList { public List&lt;SJSonModel&gt; Features { get; set; } public List&lt;SJSonModel&gt; MenuItems { get; set; } } </code></pre> <p>The controller:</p> <pre><code> [HttpPost] public ActionResult CheckPreferences(SJSonModelList postData) { BindUserFeatures(postData.Features); return Json(new { status = "Success", message = "Passed" }); } </code></pre> <p>The View simplified:</p> <pre><code>&lt;div class="Feature borderRadius Items"&gt; &lt;h2&gt;Title &lt;input type="checkbox" class="Item" name="featureName"/&gt; &lt;/h2&gt; &lt;div class="FeatureDetails subItems"&gt; &lt;a href="@Url…"&gt;featureName&lt;/a&gt; &lt;input type="checkbox" class="subItem" name="subItemName"/&gt; &lt;/div&gt; &lt;!-- endOf FeatureDetails --&gt; </code></pre> <p> </p> <p>The JQuery code:</p> <pre><code> var isChecked = false; var features = new Array(); var menuItems = new Array(); var postData = new Array(); </code></pre> <p>Here I fill the features, the menuItems with the featureName/menuItemName and isChecked boolean for each feature/menuItem</p> <pre><code>menuItems.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); features.push({ "Name": $(this).attr('name'), "isChecked": isChecked }); postData.push({ "features": features, "menuItems": menuItems }); postData = JSON.stringify(postData); </code></pre> <p>The ajax function:</p> <pre><code> $(':submit').click(function () { postData.push({ "features": features, "menuItems": menuItems }); postData = JSON.stringify(postData); $.ajax({ url: '@Url.Action("CheckPreferences")', type: 'POST', data: postData, contentType: "application/json; charset=utf-8", dataType: "json", traditional: true, success: function () { window.alert('@Resource.AjaxSuccess'); }, error: function (event, request, settings) { window.alert('@Resource.AjaxError' + ' : ' + settings); }, timeout: 20000 }); //endOf $.ajax }); //endOf :submit.click function </code></pre> <p>When I do alert(postData), in client side it contains the true values for each item but in the conroller the postData.Features and postData.MenuItems are null.</p> <p>I have tried to pass just one array to the controller too:</p> <pre><code> features = JSON.stringify(features); </code></pre> <p>in $.ajax:</p> <pre><code>{… data: features,…} </code></pre> <p>in controller:</p> <pre><code> ActionResult CheckPreferences(IEnumerable&lt;SJSonModel&gt; features) </code></pre> <p>and it works fine, but I don't know how to pass the array of json objects to my contoller. So I hope to retrieve the answer here :)</p> <p>Thank you very much.</p>
    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.
 

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