0

我有个问题。我正在尝试从列表中删除某些内容,但它从不从 .aspx.cs 调用我的删除函数

这是我的 JavaScript 代码:

function doTheDelete(doIDeleteExpenses) {
        if (selectedExpensesList.length > 0) {
            $.ajax({
                type: "POST",
                //url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
                url: '<%=ResolveUrl("~/Expenses/ViewExpenses.aspx/deleteSelectedExpense")%>',
                data: "{'DeleteExpenses' : " + "'" + doIDeleteExpenses + " '}",
                //dataaaaaa
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    var ss = data.d;
                    if (ss.length > 0) {
                        for (var i = 0; i < ss.length; ++i) {
                            $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true });
                            alert(ss[i]);

                        }
                    }
                    $("#viewTasksGrid").flexReload();
                },
                error: function (data) {
                    $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
                    alert('Error Deleting Expense');
                    if (window.console) {
                        console.log(data);
                    }
                }
            });
        } else {
            showMessage('No expenses are selected.');
        }
    }

    function getSelectedExpenseIDs() {
        var selectedExpensesList = new Array;
        var i = 0;
        $('.expenseCheckBox:checked').each(function () {
            if ($(this)[0].id !== "checkAllExpenses") {
                selectedExpensesList[i] = $(this)[0].id.split('_')[1];
                ++i;
            }
        });
        return selectedExpensesList;
    }

这是我在 aspx.cs 中永远不会被调用的方法:

[WebMethod]
public static string[] deleteSelectedExpense(bool DeleteExpenses, String[] ExpID)
{
    var rList = new List<string>();
    //var canDeleteTasks = false;
    //var canDeleteTrackers = false;
    var canDeleteExpenses = false;
    var investigatorID = (int)HttpContext.Current.Session["InvestigatorID"];
    var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
    var cmd = new SqlCommand("p_Admin_Permissions_CanDeleteExpenses", conn);
    cmd.Parameters.Add(new SqlParameter("@InvestigatorID", SqlDbType.Int));
    cmd.Parameters["@InvestigatorID"].Value = investigatorID;
    cmd.CommandType = CommandType.StoredProcedure;
    try
    {
        conn.Open();
        canDeleteExpenses = (bool)cmd.ExecuteScalar();
    }
    catch (SqlException sql)
    {
        if (!rList.Contains("Can not connect to the database. Please try again."))
            rList.Add("Can not connect to the database. Please try again.");
    }
    catch (Exception ex)
    {
        if (!rList.Contains("An Error Occured"))
            rList.Add("An Error Occured");
    }
    finally
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
    }

    if (canDeleteExpenses)
    {
        foreach (var expense in ExpID)
        {

            if (canDeleteExpenses && DeleteExpenses)
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
                cmd = new SqlCommand("p_DeleteExpenses", conn);
                cmd.Parameters.Add(new SqlParameter("@TaskID", SqlDbType.Int));
                cmd.Parameters["@ExpID"].Value = int.Parse(expense);
                cmd.Parameters.Add("@Message", SqlDbType.NVarChar, 50);
                cmd.Parameters["@Message"].Direction = ParameterDirection.Output;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException sql)
                {
                    if (!rList.Contains("Error Connecting to the Database. Unable To Delete Expense(s)."))
                        rList.Add("Error Connecting to the Database. Unable To Delete Expense(s).");
                }
                catch (Exception ex)
                {
                    if (!rList.Contains("An Error Occured"))
                        rList.Add("An Error Occured");
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
            else if (!canDeleteExpenses && DeleteExpenses)
            {
                rList.Add("You do not have permission to delete Expenses");
            }
            else
            {
                if (!rList.Contains("You do not have permission to delete the task(s)."))
                    rList.Add("You do not have permission to delete the task(s).");
            }
        }
    }
    //var serializer = new JavaScriptSerializer();
    //var re = serializer.Serialize(rList.ToArray());
    return rList.ToArray();
}

当我在浏览器控制台中时,它告诉我一个内部错误(500)。好吧,原因很明显。

4

1 回答 1

1

你的参数是错误的。该deleteSelectedExpense方法接受boolstring[]参数,但您只是将 bool 传递给它以标记是否删除它们。

编辑:我猜你没有正确传递你的字符串数组。传递字符串值数组时,json 格式应该看起来像expId : [ 1, 2, 3 ],这就是我使用该myArray.join方法并将括号放在值周围的原因。

var myArray = ['100', '200'];
$.ajax({
         type: 'POST',
         url: 'WebForm1.aspx/testMethod',
         contentType: 'application/json; charset=utf-8',
         dataType: 'json',
         data: '{ del: true, expId : [' + myArray.join(",") + '] }',
         success: success,
         error: error
       });
function success(data) {
    $('#result').html(data.d);
}
function error(data) {
    $('#result').html(data.d);
}

网络方法:

[WebMethod]
public static string testMethod(bool del, string[] expId)
{
    var rand = new Random();
    return rand.Next().ToString();
}
于 2012-09-17T20:05:14.390 回答