我有一个问题,我从选中的复选框获取一个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调用格式中的“Data:”中(我尝试了一些事情)
答案 0 :(得分:1)
尝试:
... 'TimeBill': ['" + selectedTimeTrackerList.join('","') + "']}" ...
虽然更好的解决方案是创建具有所需属性的完整javascript对象并将其作为参数apss。顺便说一下,如果你只是在这个页面上调用这个方法,你可能会考虑使用像PageMethods这样的东西而不是jQuery。
答案 1 :(得分:1)
没有理由创建要发送为Data
的字符串,您可以改为发送json对象。
Data: {
DeleteTimeTracker: doIDeleteTimeTracker,
TimeBill: selectedTimeTrackerList
}, ...
答案 2 :(得分:1)
感谢帮助男士。我设法修复它。解决方案是这样做:
"'ExpId': ['" + selectedExpensesList.join('\',\'') + "']}",
而不是:
"'ExpId': ['" + selectedExpensesList.join(',') + "']}",
添加额外'似乎有效。然后它设法分别取出每个id而不是占用整个数组并对我进行选择