0

我有一个问题,我从一个选定的复选框中获取一个 id,然后它将它发送到一个删除方法,然后它将 id 传递给一个存储过程。我遇到的问题是它以错误的格式(“232,4323”)而不是(“232”,“4323”)返回我的 id,所以在将值传递给存储过程时,它会出错因为字符串格式。这是我在 aspx 中的代码。

function doTheDelete(doIDeleteTimeTracker) {
    var selectedTimeTrackerList = getSelectedTimeTrackerIDs();
    if (selectedTimeTrackerList.length > 0) {
        $.ajax({
            type: "POST",
            //url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
            url: '<%=ResolveUrl("~/TimeTrackers/ViewTimeTrackers.aspx/deleteSelectedTimeTracker")%>',
            data: "{'DeleteTimeTracker' : '" + doIDeleteTimeTracker + "'," + "'TimeBill': ['" + selectedTimeTrackerList.join(',') + "']}",
            //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]);
                    }
                }
                $("#viewTimeTrackersGrid").flexReload();
                showMessage('Successfully Removed Time Tracker');
            },
            error: function (data) {
                $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
                alert('Error Deleting Time Tracker');
                if (window.console) {
                    console.log(data);
                }
            }
        });
    } else {
        showMessage('No time tracker(s) are selected.');
    }
}

这是我在后面代码中的删除方法。

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["connect"].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)// expense ends up beng ("232,423") instead of just taking 1 string at a time ("232").....("423")...
        {

            if (canDeleteExpenses)
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
                cmd = new SqlCommand("p_CaseFiles_Expenses_DeleteExpenses", conn);
                cmd.Parameters.Add(new SqlParameter("@ExpID", 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)
            {
                rList.Add("You do not have permission to delete Expenses");
            }
            else
            {
                if (!rList.Contains("You do not have permission to delete the Expense(s)."))
                    rList.Add("You do not have permission to delete the Expense(s).");
            }
        }
    }
    return rList.ToArray();
}

我猜它可能在数组的ajax调用格式中的“数据:”中(我已经尝试了一些事情)

4

3 回答 3

1

感谢各位大侠的帮助。我设法修复它。解决方案是这样做:

 "'ExpId': ['" + selectedExpensesList.join('\',\'') + "']}",

代替 :

 "'ExpId': ['" + selectedExpensesList.join(',') + "']}",

添加额外的 ' 似乎有效。然后它设法单独获取每个 id,而不是获取整个数组并在我身上大便

于 2012-09-19T14:58:05.500 回答
1

尝试:

... 'TimeBill': ['" + selectedTimeTrackerList.join('","') + "']}" ...

尽管更好的解决方案是创建具有所需属性的完整 javascript 对象并将其作为参数。顺便说一句,如果您仅在此页面上调用此方法,您可能需要考虑使用类似 PageMethods 而不是 jQuery 的方法。

于 2012-09-19T14:34:56.013 回答
1

没有理由创建一个要发送为的字符串Data,您可以发送一个 json 对象。

Data: { 
        DeleteTimeTracker: doIDeleteTimeTracker, 
        TimeBill: selectedTimeTrackerList 
      }, ...
于 2012-09-19T14:35:57.043 回答