激活按钮在 jQuery html 表中不起作用,而在 JSON C# 中绑定

本文关键字:而在 JSON 绑定 不起作用 激活 jQuery html 按钮 | 更新日期: 2023-09-27 17:56:16

错误,前提是此行中的输入字符串格式不正确:

objproject.CategoryStatus(Convert.ToInt32(Id), true);

这是我的 aspx 代码:

[WebMethod]
public static void ActivateSelected(String Id)
{
    clsCategoryBL objproject = new clsCategoryBL();
    string[] arr = Id.Split(',');
    foreach (var id in arr)
    {
        if (!string.IsNullOrEmpty(id))
        {
            objproject.CategoryStatus(Convert.ToInt32(Id), true);
        }
    }
}

这是我的jQuery绑定代码:

function ActivateSelected()
{
    var ids = '';
    var cells = Array.prototype.slice.call(document.getElementById("example1").getElementsByTagName('td'));
    debugger;
    for (var i in cells) {
        var inputArray = cells[i].getElementsByTagName('input');
        for (var i = 0; i < inputArray.length; i++) {
            if (inputArray[i].type == 'checkbox' && inputArray[i].checked == true) {
                debugger;
                ids += inputArray[i].id + ',';
            }
        }
    }
    debugger;
    var urldata = "WebForm5.aspx/ActivateSelected";
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: "true",
        cache: "false",
        url: urldata,
        data: "{Id:'" + ids + "'}",
        success: function (dt)
        {
            debugger;
            debugger;
            $("#example1").DataTable();
            //$("#example1").bind;
            debugger;
        },
        error: function (result) {
            alert("Error");
            //console.log();
            //alert(result);
        }
    });
}

这是我的BL课:

  public string CategoryStatus(int CategoryID, bool Status)
    {
        using (KSoftEntities db = new KSoftEntities())
        {
            try
            {
                var ProjectDetails = db.tblCategories.Where(i => i.CategoryID == CategoryID).ToList();
                ProjectDetails[0].Status = Status;
                db.SaveChanges();
                if (Status == true)
                    return "Record Activated successfully";
                else
                    return "Record deactivated successfully";
            }
            catch (Exception ex)
            {
            }
            return "Error on updation";
        }
    }

激活按钮在 jQuery html 表中不起作用,而在 JSON C# 中绑定

最后一个 ID 附加了额外的逗号,

比如:"1234,123,12345,"

上面示例的拆分字符串将创建 4 个元素的数组,最后一个元素中有空字符串。这就是为什么它的抛出错误。

你需要在jquery中处理ids += inputArray[i].id + ',';在这里,您可以检查ID是否是列表中的最后一个ID,之后是否需要逗号。

function ActivateSelected()
{
var ids = '';
var cells = Array.prototype.slice.call(document.getElementById("example1").getElementsByTagName('td'));
debugger;
for (var i in cells) {
    var inputArray = cells[i].getElementsByTagName('input');
    for (var i = 0; i < inputArray.length; i++) {
        if (inputArray[i].type == 'checkbox' && inputArray[i].checked == true) {
            debugger;
            ids += inputArray[i].id + ',';
        }
    }
}
ids = ids.substring(0,ids.lastIndexOf(','));
debugger;
var urldata = "WebForm5.aspx/ActivateSelected";
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: "true",
    cache: "false",
    url: urldata,
    data: "{Id:'" + ids + "'}",
    success: function (dt)
    {
        debugger;
        debugger;
        $("#example1").DataTable();
        //$("#example1").bind;
        debugger;
    },
    error: function (result) {
        alert("Error");
        //console.log();
        //alert(result);
    }
});

}