我有一个问题,我从一个选定的复选框中获取一个 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调用格式中的“数据:”中(我已经尝试了一些事情)