1

I have a JSON object with nested arrays which I would like to send to a controller.

This is my jQuery.ajax call:

$.ajax({
            url: "@Url.Action("ExportJson")",
            type: "POST",
            data: JSON.stringify(myObj),
            contentType:"application/json",
            success: function (result) {

            }
        });

Controller:

public ActionResult ExportJson(string json)
            {



                return null;
            }

Why is the json string coming back as null in the controller? Whereas console.log(JSON.stringify(myObj)) shows the correct object in browser console.

user5120455
  • 141
  • 2
  • 4
  • 15
  • 3
    There are similar questions [here](http://stackoverflow.com/questions/21578814/how-to-receive-json-as-an-mvc-5-action-method-parameter), [here](http://stackoverflow.com/questions/4164114/posting-json-data-to-asp-net-mvc) and [here](http://stackoverflow.com/questions/15317856/asp-net-mvc-posting-json). – Jasen Jan 03 '17 at 20:38
  • I just need the raw json string without the ViewModel creation – user5120455 Jan 03 '17 at 20:39
  • 2
    There are answers in the linked questions which note how to do that. For instance, on the first link: http://stackoverflow.com/a/21579180/215552 – Heretic Monkey Jan 03 '17 at 20:43
  • @HereticMonkey Thanks for the link but the link doesn't have info on how to do this without model creation but using contentType: "application/json; charset=utf-8" in client request headers. Have any idea on this? – Teja duggirala Apr 11 '23 at 10:32
  • @Tejaduggirala That wasn't the question in this case. If you have a question with different requirements, and you can't find a duplicate, ask a new question. – Heretic Monkey Apr 11 '23 at 13:33

2 Answers2

1

Try This -

Consider the following JSON data. Let's assume that the json data is obtained from the whatever form you're trying to submit.

var jsonData =  {"FirstName":"John", "LastName":"Doe", "DoB":"01/01/1970", 
                  [{"CompanyName":"Microsoft", "StartDate":"01/01/2010", "EndDate":"01/01/2011"},                      
                   {"CompanyName":"Apple Inc", "StartDate":"01/01/2011", "EndDate":"01/01/2012"}
                ]};

The below ajax method should get you going. Make sure you specify POST type, as ajax method uses GET method by default.

$.ajax({
  url:"@Url.Action("ExportJson")",
  data: jsonData, //this could also be form data
  type:"POST",
  success:function(data){
     //Do something:
  }})
  .done(function(data){
    //data - response from server after save
  })
  .fail(){
    alert("ajax error")
  });

MVC Controller: Decorate the Action method with HttpPost verb. This action method will only handle http post request from browser. Ajax submission from the browser will be automatically deserialized to FormData c# class as a poco. As a result you should be able to consume the jsnData object without any modifications.

[HttpPost]
public ActionResult ExportJson(FormData jsnData)
{
     //do something here
}

FormData class would be C# class representing the json data:

public class FormData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DoB { get; set; }
    public List<WorkExperience> workExperience { get; set; }
}

public class WorkExperience 
{
   public string CompanyName { get; set;}
   public DateTime StartDate { get; set; }
   public DateTime EndDate { get; set; }       
} 
DragoRaptor
  • 735
  • 5
  • 10
0

Change the ajax post like this;

$.ajax({
        url: "@Url.Action("ExportJson")",
        type: "POST",
 //************************************
        data: {"json" : JSON.stringify(myObj)},
 //************************************
        contentType:"application/json",
        success: function (result) {

        }
    });
Serhat MERCAN
  • 1,078
  • 3
  • 14
  • 31