html表jQuery json值传递

时间:2016-07-13 14:08:40

标签: c# jquery

我想通过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;

    }

该值未通过webmethod ...

1 个答案:

答案 0 :(得分:0)

你的后端需要一个参数catid所以你必须将catid传递给AJAX调用@BenG和@palash回答数据属性。

data: { catid: 1 } 

或者您将其传递给格式为

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

否则,它将无法工作,因为BindCategoryDatatable返回一个空列表。