Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've found an solution. I use an solution of Steve Gentile, <em><a href="http://blogger.forgottenskies.com/?p=252" rel="noreferrer">jQuery and ASP.NET MVC – sending JSON to an Action – Revisited</a></em>.</p> <p>My ASP.NET MVC view code looks like:</p> <pre><code>function getplaceholders() { var placeholders = $('.ui-sortable'); var results = new Array(); placeholders.each(function() { var ph = $(this).attr('id'); var sections = $(this).find('.sort'); var section; sections.each(function(i, item) { var sid = $(item).attr('id'); var o = { 'SectionId': sid, 'Placeholder': ph, 'Position': i }; results.push(o); }); }); var postData = { widgets: results }; var widgets = results; $.ajax({ url: '/portal/Designer.mvc/SaveOrUpdate', type: 'POST', dataType: 'json', data: $.toJSON(widgets), contentType: 'application/json; charset=utf-8', success: function(result) { alert(result.Result); } }); }; </code></pre> <p>and my controller action is decorated with an custom attribute</p> <pre><code>[JsonFilter(Param = "widgets", JsonDataType = typeof(List&lt;PageDesignWidget&gt;))] public JsonResult SaveOrUpdate(List&lt;PageDesignWidget&gt; widgets </code></pre> <p>Code for the custom attribute can be found <a href="http://www.forgottenskies.com/MvcJson.zip" rel="noreferrer">here</a> (the link is broken now).</p> <p>Because the link is broken this is the code for the JsonFilterAttribute</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>JsonConvert.DeserializeObject is from Json.NET</p> <p>Link: <a href="http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm" rel="noreferrer">Serializing and Deserializing JSON with Json.NET</a></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. 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