Q:

ASP.NET MVC posting json with inserted object

How can I post json object with inserted object?
My code:
Models code:
   public class MyModel
    {
        public string Text { set; get; }
        public int Param { set; get; }
        public InsertedModel insert { set; get; }
    }
public class InsertedModel
    {
        public string InsertedString { set; get; }
    }

JavaScript code:
<script>
    function createPost() {
        $.ajax({
            type: "POST", url: "/",
            success: function (data) { alert('data: ' + data); },
            data: { "Text": "Some Name", "Param": 30, "insert": { "InsertedString": "123" } },
            accept: 'application/json'
        });
    }

</script>

Controller code:
   [HttpPost]
    public void Index(Models.MyModel postReq)
    {
         //breakpoint
    }

In breakpoint i see:
Param: 30
Text: "Some Name"
insert: {Models\InsertedModel}
insert.InsertedString: null

Please help me. What I need to do with this? How can I see all my JSON struct in C# HTTPPost function?

A:

You are just passing the object instead of JSON. Better convert this object to a string and specify the content type as JSON:
var requestData = JSON.stringify({ "Text": "Some Name", "Param": 30, "insert": { "InsertedString": "123" } });
function createPost() {
    $.ajax({
        type: "POST", url: "/",
        success: function (data) { alert('data: ' + data); },
        contentType: 'application/json',
        data: requestData,
        accept: 'application/json'
    });
}

