I have multiple arrays that I want to pass from view into a controller method. For that purpose, I converted those arrays into JSON objects. Then, create the AJAX call, but how do I send those JSON objects at once?
var json_InstallationControl = JSON.stringify(array_installationControl);
var json_HardwareGUID = JSON.stringify(array_HardwareGUID);
var json_InstallAppID = JSON.stringify(array_InstallAppID);
var json_MACAddress = json.stringify(array_MACAddress);
$.ajax({
url: "@Url.Content("~/Home/ActivationManagement")",
type: "POST",
contentType: "application/json",
data: { jsonData: json_InstallationControl },
success: function(){
console.log('success!!');
}
})
[HttpPost]
public ActionResult ActivationManagement(String jsonData)
Like this :
and on server
Or if you want to send a single object to server, create new object with all 4 arrays as properties and then stringify and send that object.
As always start by writing a view model that will hold the information:
that your controller action will take:
and now all that's left is to send the corresponding JSON object:
In this example I have used string arrays as properties to the view model but you could of course use arbitrarily complex objects depending on the data you are trying to send.