[英]Pass array from client side to service side c#
I am calling a javascript function on button click using onclientclick with below function. 我正在使用带有以下功能的onclientclick在按钮单击上调用javascript函数。
function addValues() {
debugger;
var arrValue = [];
var hdnValue = document.getElementById("hdn").value;
var strValue = hdnValue.split(',');
for (var i = 0; i < strValue.length; i++) {
var ddlValue = document.getElementById(strValue[i]).value;
arrValue.push(ddlValue);
}
}
arrValue
array will have all the required values and how can I move this array values to server side for further process. arrValue
数组将具有所有必需的值,以及如何将该数组值移到服务器端以进行进一步处理。
Update 1: 更新1:
HTML: HTML:
function addValues() {
debugger;
var arrddlValue = [];
var hdnddlValue = document.getElementById("hdnDDL").value;
var strddlValue = hdnddlValue.split(',');
for (var i = 0; i < strddlValue.length; i++) {
var ddlValue = document.getElementById(strddlValue[i]).value;
arrddlValue.push(ddlValue);
}
}
$.ajax({
url: '',
data: { ddlArray: arrddlValue },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
},
error: function (x, e) {
}
});
Code: 码:
protected void btnSort_Click(object sender, EventArgs e)
{
try
{
if (Request["ddlArray[]"] != null)
{
string[] arrValues = Array.ConvertAll(Request["ddlArray[]"].ToString().Split(','), s => (s));
}
}
}
If your framework is ASP.Net you can pass it by $.ajax
, I am passing array like: 如果您的框架是ASP.Net,则可以通过$.ajax
传递它,我传递的数组如下:
$.ajax({
url: '',
data: { AbArray: arrValue },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data) {
},
error: function (x, e) {
}
}); });
and get it in Back-end like: 并像下面这样在后端中获取它:
if (request["AbArray[]"] != null)
{
int[] arrValues = Array.ConvertAll(request["AbArray[]"].ToString().Split(','), s => int.Parse(s));
}
suppose array is int
. 假设数组是int
。
the above sample is using Generic-Handler
. 上面的示例使用Generic-Handler
。
if you want to use webmethod
do something like: 如果您想使用webmethod
请执行以下操作:
[WebMethod(EnableSession = true)]
public static void PassArray(List<int> arr)
{
}
and Ajax would be like: 而Ajax就像:
function addValues() {
debugger;
var arrddlValue = [];
var hdnddlValue = document.getElementById("hdnDDL").value;
var strddlValue = hdnddlValue.split(',');
for (var i = 0; i < strddlValue.length; i++) {
var ddlValue = document.getElementById(strddlValue[i]).value;
arrddlValue.push(ddlValue);
}
var jsonVal = JSON.stringify({ arr: arrValue });
$.ajax({
url: 'YourPage.aspx/PassArray',
data: jsonVal,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data) {
},
error: function (x, e) {
}
});
}
Change Ajax Url as your PassArray
address which mean YourPage.aspx
should be change to page name which have PassArray
in it's code-behind. 将Ajax Url更改为您的PassArray
地址,这意味着YourPage.aspx
应该更改为代码名称中带有PassArray
页面名称。
If you are using Asp.net WebForm Application, 如果您使用的是Asp.net WebForm应用程序,
Approach 1 : you can store your array value in a hidden input control and retrieve the saved data in your c# coding. 方法1:您可以将数组值存储在隐藏的输入控件中,并以c#编码检索保存的数据。
Approach 2 : define web method in your server side c# code and pass this javascript array value as ajax call. 方法2:在服务器端C#代码中定义Web方法,并将此javascript数组值作为ajax调用传递。
link for Approach 1 : https://www.aspsnippets.com/Articles/Pass-JavaScript-variable-value-to-Server-Side-Code-Behind-in-ASPNet-using-C-and-VBNet.aspx 方法1的链接: https : //www.aspsnippets.com/Articles/Pass-JavaScript-variable-value-to-Server-Side-Code-Behind-in-ASPNet-using-C-and-VBNet.aspx
link for Approach 2 : https://www.aspsnippets.com/Articles/Send-and-receive-JavaScript-Array-to-Web-Service-Web-Method-using-ASP.Net-AJAX.aspx 方法2的链接: https : //www.aspsnippets.com/Articles/Send-and-receive-JavaScript-Array-to-Web-Service-Web-Method-using-ASP.Net-AJAX.aspx
I would "stringify" the array by imploding it with a special char unlikely to appear in my values (for example: §), and then with the help of jQuery.ajax() function, I will send it to the backend (ASP.NET MVC) action method: 我将通过用不太可能出现在我的值中的特殊字符(例如:§)将其内插来“字符串化”该数组,然后借助jQuery.ajax()函数,将其发送到后端(ASP。 NET MVC)操作方法:
$.ajax({
url : 'http://a-domain.com/MyController/MyAction',
type : 'POST'
data : 'data=' + myStringifiedArray;
});
My backend would be something like this (in MyController class): 我的后端将是这样的(在MyController类中):
[HttpPost]
public ActionResult MyAction(string data)
{
string[] arrValue = data.Split('§');
...
}
UPDATE UPDATE
For ASP.NET forms, the ajax request would be: 对于ASP.NET表单,ajax请求将是:
$.ajax({
url : 'http://a-domain.com/MyPage.aspx/MyMethod',
type : 'POST'
data : 'data=' + myStringifiedArray;
});
And the backend would be something like this: 后端将是这样的:
[System.Web.Services.WebMethod]
public static void MyMethod(string data)
{
string[] arrValue = data.Split('§');
...
}
You will find a more precise explanation here: https://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx 您将在这里找到更精确的解释: https : //www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.