html table jQuery json value pass

本文关键字:value pass json jQuery table html | 更新日期: 2023-09-27 17:56:57

我想通过jQuery Json传递值.问题是在传递值时,它有一些问题,这就是为什么它不调用c#代码。这是 html 表:

     $(function ()
      {
          debugger
          $.ajax({
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "WebForm7.aspx/BindCategoryDatatable",
              data: "{}",
              dataType: "json",
              success: function (dt) {
                  debugger;
                  for (var i = 0; i < dt.d.length; i++) {
                        $("#tblid > tbody").append("<tr><td> <input type='checkbox' class='chk' id=" + dt.d[i].projectid + " /></td><td>" + dt.d[i].projectname + "</td><td>" + dt.d[i].Name + "</td><td>" + dt.d[i].technology + "</td><td>" + dt.d[i].projectliveurl + "</td><td><input type='image' src=" + dt.d[i].StatusImage + " onclick='actvstatus(" + dt.d[i].projectid + ", " + dt.d[i].status + ")' alt='Submit' width='18' height='18'>  </td><td>" + dt.d[i].DisplayOrder + "</td><td> <i class='ui-tooltip fa fa-pencil' onclick='btnQueryString_Click(" + dt.d[i].projectid + ")' style='font-size:22px;margin-left: 32px;'></i><i class='ui-tooltip fa fa-trash-o' onclick='deleteRecord(" + dt.d[i].projectid + ")' style='font-size: 22px;margin-left: 32px;color:red'></i> </tr>");
                  }
                  $("#tblid").DataTable();
              },
              error: function (result) {
                  alert("Error");
              }
          });

      });

这是我的 c# 代码:

[WebMethod]
    public static List<mProjects> BindCategoryDatatable(int catid)
    {
        clsProjectBL objcategory = new clsProjectBL();
        List<mProjects> modelCategory = new List<mProjects>();
        DataTable dtCategory = new DataTable();
        //dtCategory = objcategory.GetAllCategoryDetails("admin");
         dtCategory = objcategory.GetAllProjectDetails("admin", catid);
        if (dtCategory.Rows.Count > 0)
        {
            modelCategory = (from DataRow dr in dtCategory.Rows
                             select new mProjects()
                             {
                                 projectid = Convert.ToInt32(dr["projectid"]),
                                 CategoryID = Convert.ToInt32(dr["CategoryID"]),
                                 projectname = dr["projectname"].ToString(),
                                 Name = dr["Name"].ToString(),
                                 technology = dr["technology"].ToString(),
                                 projectliveurl = dr["projectliveurl"].ToString(),
                                  DisplayOrder = Convert.ToInt32(dr["DisplayOrder"]),
                                 status = Convert.ToBoolean(dr["status"]),
                                 StatusImage = dr["StatusImage"].ToString(),
                                 //Deleted = Convert.ToBoolean(dr["Deleted"])
                             }).ToList();

        }
        return modelCategory;
    }

该值未通过网络方法传递...

html table jQuery json value pass

后端需要一个参数 catid,因此您必须将 catid 作为 @BenG传递给 AJAX 调用,并使用数据属性@palash答案。

data: { catid: 1 } 

或者你用格式把它传递给网址

url: "WebForm7.aspx/BindCategoryDatatable?catid=" + YOUR_ID.

否则,它将不起作用,因为 BindCategoryDatatable 返回一个空列表。