【问题标题】:How to pass a JavaScript array to C# function using Ajax如何使用 Ajax 将 JavaScript 数组传递给 C# 函数
【发布时间】:2016-11-05 02:40:09
【问题描述】:

我在按钮单击时传递了 JavaScript 中的字符串数组,在此处单击按钮时,dataArray 存储表中第一个元素的字符串值,其中一些行已被选择,之后我将 stringify 作为 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);
    });

dataArray 看起来像这样

["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();
            }
        }
    }
}

它提供了 500 个内部服务器

【问题讨论】:

  • 在您的 ajax 请求选项中设置 traditional: true
  • 不工作添加该行

标签: javascript c# jquery asp.net ajax


【解决方案1】:

这会起作用(在 javascript 中)

       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
}

【讨论】:

    【解决方案2】:

    在将 JSON 发送到 WebMethod 之前始终将其字符串化

    data: JSON.stringify({ Students: dataArr })
    

    【讨论】:

    • 非常感谢 :) 正在使用 stringify 但没有将其发送到 ajax 函数
    【解决方案3】:

    List&lt;string&gt; data试试

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

    其他

     [WebMethod]
    public static void DeleteStudent(List<string> data)
    {   
    
       data: { data : dataArr },
    

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 1970-01-01
      • 2014-06-21
      • 2021-02-10
      • 2011-11-07
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多