最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing JSON to MVC Controller? - Stack Overflow

programmeradmin1浏览0评论

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 ing back as null in the controller? Whereas console.log(JSON.stringify(myObj)) shows the correct object in browser console.

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 ing back as null in the controller? Whereas console.log(JSON.stringify(myObj)) shows the correct object in browser console.

Share Improve this question asked Jan 3, 2017 at 20:26 user5120455user5120455 1412 gold badges4 silver badges15 bronze badges 5
  • 3 There are similar questions here, here and here. – Jasen Commented Jan 3, 2017 at 20:38
  • I just need the raw json string without the ViewModel creation – user5120455 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./a/21579180/215552 – Heretic Monkey 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? – Teja duggirala 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. – Heretic Monkey Commented Apr 11, 2023 at 13:33
Add a ment  | 

2 Answers 2

Reset to default 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; }       
} 

Change the ajax post like this;

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

        }
    });
发布评论

评论列表(0)

  1. 暂无评论