Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to use a <code>CreateJSONObj</code> method or a <code>jsonModel</code> property on your model. In order to use it in the view you could simply use the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="noreferrer">JavaScriptSerializer</a> class which will convert the server side model object into a javascript object:</p> <pre><code>&lt;script type="text/javascript"&gt; var jsModel = @Html.Raw(new JavaScriptSerializer().Serialize(Model)); $.ajax({ url: '@Url.Action("SetJSON", "Page")', type: 'POST', data: JSON.stringify(jsModel), contentType: 'application/json; charset=utf-8', success: function () { $('#test').html('Saved').fadeIn(); }, error: function () { $('#test').html('error'); } }); &lt;/script&gt; </code></pre> <p>This will successfully send the model to the following controller action:</p> <pre><code>[HttpPost] public ActionResult SetJSON(Model model) { ... } </code></pre> <p>where the <code>Model</code> class contains all the necessary information:</p> <pre><code>public class Model { public string Name { get; set; } public IEnumerable&lt;Item&gt; Items { get; set; } } public class Item { public int Test { get; set; } } </code></pre> <p>and the controller:</p> <pre><code>public class PageController: Controller { // Used to render the view public class Index() { var model = new Model { Name = "Test", Items = new[] { new Item { Test = 1 }, new Item { Test = 2 }, } }; return View(model); } // Used to handle the AJAX POST request [HttpPost] public ActionResult SetJSON(Model model) { ... } } </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. 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