2

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)
1
  • how to do you use jsonify? Commented Aug 21, 2013 at 16:38

2 Answers 2

11

As always start by writing a view model that will hold the information:

public class MyViewModel
{
    public string[] InstallationControls { get; set; }
    public string[] HardwareGUIDs { get; set; }
    public string[] InstallAppIDs { get; set; }
    public string[] MACAddresses { get; set; }
}

that your controller action will take:

[HttpPost]
public ActionResult ActivationManagement(MyViewModel model)
{
    ...
}

and now all that's left is to send the corresponding JSON object:

var data = JSON.stringify({
    installationControls: array_installationControl,
    hardwareGUIDs: array_HardwareGUID,
    installAppIDs: array_InstallAppID,
    macAddresses: array_MACAddress
});

$.ajax({
    url: "@Url.Content("~/Home/ActivationManagement")",
    type: "POST",
    contentType: "application/json",
    data: { data: data },
    success: function() {
        console.log('success!!');
    }
});

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.

Sign up to request clarification or add additional context in comments.

4 Comments

beat me too it; copy-paster!
I am just wondering, will this work if I have multiple parameter in the controller method? Or does it have to be only one parameter in the method? Thanks
But why would you have multiple parameters in a controller method? The whole point of my answer is that you should write a view model that will contain all the necessary information. So if you needed something else, just add them as properties to the view model. This way you never need to add more than one parameter to a controller action.
Good stuff @DarinDimitrov, worked for me after I changed the ajax request from - "data: { data: data }," to - "data: data," I guess it was because MyViewModel included IEnumerable type and int type and not included a string arrays.
0

Like this :

$.ajax({
url: "@Url.Content("~/Home/ActivationManagement")",
type: "POST",
contentType: "application/json",
data: {
 json_InstallationControl: json_InstallationControl,
json_HardwareGUID :json_HardwareGUID,
json_InstallAppID :json_InstallAppID,
json_MACAddress :json_MACAddress 
},
success: function(){
console.log('success!!');
}
})

and on server

public ActionResult ActivationManagement(String json_InstallationControl,String json_HardwareGUID ,String json_InstallAppID,String json_MACAddress )

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.

3 Comments

But this would mean that you need to JSON deserialize the data in your controller action which IMHO is an unnecessary plumbing code which should be left to the framework instead of polluting your controller code with it.
There is @RequestBody annotation in spring that deserializes JSON and creates a java object, was not sure how its done in asp.net so didnt include it.
But this question is about ASP.NET MVC 4, not about the Spring framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.