Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Ajax将JavaScript数组传递给C#函数_Javascript_C#_Jquery_Asp.net_Ajax - Fatal编程技术网

如何使用Ajax将JavaScript数组传递给C#函数

如何使用Ajax将JavaScript数组传递给C#函数,javascript,c#,jquery,asp.net,ajax,Javascript,C#,Jquery,Asp.net,Ajax,我在按钮单击上传递了JavaScript中的字符串数组,这里在按钮单击上,dataArray存储表中第一个元素的字符串值,在该表中选择了一些行之后,我将字符串化为JSON,并调用Ajax函数将数据发送到我的代码隐藏函数DeleteStudent。 我在单击按钮时调用的JavaScript函数: $('#deleteStudent').click(function () { var dataArr = []; $.each($("#StudentTa

我在按钮单击上传递了JavaScript中的字符串数组,这里在按钮单击上,dataArray存储表中第一个元素的字符串值,在该表中选择了一些行之后,我将字符串化为JSON,并调用Ajax函数将数据发送到我的代码隐藏函数DeleteStudent。
我在单击按钮时调用的JavaScript函数:

$('#deleteStudent').click(function () {
            var dataArr = [];
            $.each($("#StudentTable tr.selected"), function () {
                dataArr.push($(this).find('td').eq(0).text()); 
            });
            var StudentList = JSON.stringify(dataArr);
            $.ajax({
                type: "POST",
                url: "ViewStudents.aspx/DeleteStudent",
                contentType: "application/json; charset=utf-8",
                data: { Students: dataArr },
                dataType: "json",
                traditional: true,
                success: function (result) {
                    alert('Yay! It worked!');
                },
                error: function (result) {
                    alert('Oh no :(  : '+result);
                }
            });
            console.log(StudentList);
    });
数据数组如下所示

["10363","10364","10366"]
代码隐藏功能:

[WebMethod]
public static void DeleteStudent(string[] Students)
{
    Console.WriteLine("Reached CS");
    string[] a =Students;
    for (int i = 0; i < a.Length; i++)
    {
        string admissionNumber=a[i];
        using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
        {
            using (MySqlCommand deleteStudent = new MySqlCommand())
            {
                deleteStudent.CommandType = CommandType.Text;
                deleteStudent.Connection = conn;
                deleteStudent.CommandText = "DELETE FROM validstudents WHERE admissionNumber = @admissionNumber ";

                deleteStudent.Parameters.AddWithValue("@admissionNumber", admissionNumber);

                conn.Open();
                deleteStudent.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
}
[WebMethod]
公共静态无效删除学生(字符串[]学生)
{
控制台写入线(“到达CS”);
字符串[]a=学生;
for(int i=0;i
它提供了500个内部服务器

Try by
列表数据

[WebMethod]
public static void DeleteStudent(string[] data)
{
否则

[WebMethod]
公共静态无效删除学生(列表数据)
{   
数据:{data:dataArr},

在将JSON发送到
WebMethod

data: JSON.stringify({ Students: dataArr })
这将起作用(在javascript中)


在ajax请求选项中设置
traditional:true
。不工作添加了LineThank alot:)正在使用stringify,但没有将其发送到ajax函数
data: JSON.stringify({ Students: dataArr })
       var optionSelected ="me"
                    var id = { id: optionSelected };

                    $.ajax({
                        url: '@Url.Action("GetConnectionProvider", "Customers")',
                        contentType: "application/json;charset=utf-8",
                        data: JSON.stringify(id),
                        type: 'POST',
                        dataType: 'json',
                        success: function(datas) {


                        }
                    });
In Action

 public ActionResult GetConnectionProvider(int id)
{
   //write your code
}