2

我有多个要从视图传递到控制器方法的数组。为此,我将这些数组转换为 JSON 对象。然后,创建 AJAX 调用,但如何一次发送这些 JSON 对象?

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)
4

2 回答 2

10

与往常一样,首先编写一个包含信息的视图模型:

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

您的控制器将采取的行动:

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

现在剩下的就是发送相应的 JSON 对象:

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!!');
    }
});

在此示例中,我使用字符串数组作为视图模型的属性,但您当然可以根据您尝试发送的数据使用任意复杂的对象。

于 2013-08-21T16:38:25.393 回答
0

像这样 :

$.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!!');
}
})

并在服务器上

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

或者,如果您想将单个对象发送到服务器,请创建具有所有 4 个数组作为属性的新对象,然后将该对象字符串化并发送。

于 2013-08-21T16:38:42.800 回答