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.

5
  • 3
    There are similar questions here, here and here. Commented Jan 3, 2017 at 20:38
  • I just need the raw json string without the ViewModel creation Commented Jan 3, 2017 at 20:39
  • 2
    There are answers in the linked questions which note how to do that. For instance, on the first link: stackoverflow.com/a/21579180/215552 Commented Jan 3, 2017 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? Commented Apr 11, 2023 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. Commented Apr 11, 2023 at 13:33

2 Answers 2

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; }       
} 
Sign up to request clarification or add additional context in comments.

Comments

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) {

        }
    });

1 Comment

When using POST it gives error "Invalid json primitive" so, change ajax type: "POST" to "GET".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.