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; }
}