如何在一个Ajax调用传递多个阵列控制器的方法ASP.net MVC 4 [英] How to pass multiple arrays in one ajax call to controller method ASP.net MVC 4

查看:107
本文介绍了如何在一个Ajax调用传递多个阵列控制器的方法ASP.net MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想从视图传递到控制器方法,多个磁盘阵列。为此,我转换这些阵列成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);

$阿贾克斯({
网址:@ Url.Content(〜/主页/ Activati​​onManagement),
键入:POST,
的contentType:应用/ JSON
数据:{jsonData:json_InstallationControl},
成功:函数(){
的console.log('成功!');
}
})

[HttpPost]
公众的ActionResult Activati​​onManagement(字符串jsonData)
 

解决方案

与往常一样通过编写视图模型,将举行信息启动:

 公共类MyViewModel
{
    公共字符串[] InstallationControls {获得;组; }
    公共字符串[] HardwareGUIDs {获得;组; }
    公共字符串[] InstallAppIDs {获得;组; }
    公共字符串[] MACAddresses {获得;组; }
}
 

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

  [HttpPost]
公众的ActionResult Activati​​onManagement(MyViewModel模型)
{
    ...
}
 

现在所有剩下的就是进行相应的JSON对象:

  VAR数据= JSON.stringify({
    installationControls:array_installationControl,
    hardwareGUIDs:array_HardwareGUID,
    installAppIDs:array_InstallAppID,
    macAddresses:array_MACAddress
});

$阿贾克斯({
    网址:@ Url.Content(〜/主页/ Activati​​onManagement),
    键入:POST,
    的contentType:应用/ JSON
    数据:{数据:数据},
    成功:函数(){
        的console.log('成功!');
    }
});
 

在这个例子中,我使用了字符串数组作为属性到视图模型,但当然,你可以使用取决于你要发送的数据的任意复杂的对象。

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)

解决方案

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.

这篇关于如何在一个Ajax调用传递多个阵列控制器的方法ASP.net MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆