i have a problem. I am trying to delete something from a list but it never calls my delete function from the .aspx.cs
我有一个问题。我正在尝试从列表中删除一些东西,但它从来不从。aspx.cs中调用我的删除函数。
here is my javascript code:
下面是我的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;
}
and here is my method in the aspx.cs that never gets called:
这是我在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();
}
When I am in the browser console, it tells gives me an internal error(500). Well for obvious reasons.
当我在浏览器控制台时,它会告诉我一个内部错误(500)。原因很明显。
1 个解决方案
#1
1
Your parameters are wrong. The deleteSelectedExpense
method takes bool
and string[]
parameters, but you're only passing it the bool to flag wether to delete them.
你的参数是错误的。deleteSelectedExpense方法接受bool和string[]参数,但您只是将bool传递给它以标记是否要删除它们。
EDIT: You're not passing your string array properly I'm guessing. When passing the array of string values, the json format should look like expId : [ 1, 2, 3 ]
, which is why I'm using the myArray.join
method and placing the brackets around the values.
编辑:我猜你没有正确地传递你的字符串数组。在传递字符串值数组时,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:
WebMethod:
[WebMethod]
public static string testMethod(bool del, string[] expId)
{
var rand = new Random();
return rand.Next().ToString();
}
#1
1
Your parameters are wrong. The deleteSelectedExpense
method takes bool
and string[]
parameters, but you're only passing it the bool to flag wether to delete them.
你的参数是错误的。deleteSelectedExpense方法接受bool和string[]参数,但您只是将bool传递给它以标记是否要删除它们。
EDIT: You're not passing your string array properly I'm guessing. When passing the array of string values, the json format should look like expId : [ 1, 2, 3 ]
, which is why I'm using the myArray.join
method and placing the brackets around the values.
编辑:我猜你没有正确地传递你的字符串数组。在传递字符串值数组时,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:
WebMethod:
[WebMethod]
public static string testMethod(bool del, string[] expId)
{
var rand = new Random();
return rand.Next().ToString();
}