Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First download this JavaScript code, <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js" rel="noreferrer">JSON2.js</a>, that will help us serialize the object into a string.</p> <p>In my example I'm posting the rows of a <a href="http://www.trirand.com/jqgridwiki/doku.php?id=start" rel="noreferrer">jqGrid</a> via Ajax:</p> <pre><code> var commissions = new Array(); // Do several row data and do some push. In this example is just one push. var rowData = $(GRID_AGENTS).getRowData(ids[i]); commissions.push(rowData); $.ajax({ type: "POST", traditional: true, url: '&lt;%= Url.Content("~/") %&gt;' + AREA + CONTROLLER + 'SubmitCommissions', async: true, data: JSON.stringify(commissions), dataType: "json", contentType: 'application/json; charset=utf-8', success: function (data) { if (data.Result) { jQuery(GRID_AGENTS).trigger('reloadGrid'); } else { jAlert("A problem ocurred during updating", "Commissions Report"); } } }); </code></pre> <p>Now on the controller:</p> <pre><code> [HttpPost] [JsonFilter(Param = "commissions", JsonDataType = typeof(List&lt;CommissionsJs&gt;))] public ActionResult SubmitCommissions(List&lt;CommissionsJs&gt; commissions) { var result = dosomething(commissions); var jsonData = new { Result = true, Message = "Success" }; if (result &lt; 1) { jsonData = new { Result = false, Message = "Problem" }; } return Json(jsonData); } </code></pre> <p>Create a JsonFilter Class (thanks to JSC reference).</p> <pre><code> public class JsonFilter : ActionFilterAttribute { public string Param { get; set; } public Type JsonDataType { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.ContentType.Contains("application/json")) { string inputContent; using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream)) { inputContent = sr.ReadToEnd(); } var result = JsonConvert.DeserializeObject(inputContent, JsonDataType); filterContext.ActionParameters[Param] = result; } } } </code></pre> <p>Create another class so the filter can parse the JSON string to the actual manipulable object: This class comissionsJS are all the rows of my jqGrid.</p> <pre><code> public class CommissionsJs { public string Amount { get; set; } public string CheckNumber { get; set; } public string Contract { get; set; } public string DatePayed { get; set; } public string DealerName { get; set; } public string ID { get; set; } public string IdAgentPayment { get; set; } public string Notes { get; set; } public string PaymentMethodName { get; set; } public string RowNumber { get; set; } public string AgentId { get; set; } } </code></pre> <p>I hope this example helps to illustrate how to post a complex object.</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.
    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.
    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