简体   繁体   中英

Posting Json data to controller

I am new to .Net MVC. I am trying to send some data from my view to the controller.

Controller Code:

    [HttpPost]
    public ActionResult Add(string json)
    {
        //more code here
    }

JS:

function SaveDetails() {
     var details= {
    "Code": "test",
    "Desc": "Testing",
    "Xclude": "N"
};
$.ajax({
    type: "POST",
    async: true,
    url: "Add",
    contentType: 'application/json',
    dataType: "json",
    data: JSON.stringify(details),
    success: function (){
                $('#Code').val("");
                $('#Desc').val("");
                $('#Xclude')[0].checked = false;
    }
    });

}

But when I debug the code, the variable json in the controller is getting null value and not the data I am passing to it. I am not able to identify what is wrong here. Any help would be much appreciated.

Try:

data: {json:JSON.stringify(details)}

Otherwise MVC is going to expect the parameter name to be id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM