【问题标题】:passing string array from ajax to c# web method将字符串数组从 ajax 传递给 c# web 方法
【发布时间】:2013-12-19 13:13:43
【问题描述】:

您好,下面有这段代码

我如何最好传递 1 个数组,该数组将包含一个 ID 号和一个文本框中的值,该文本框是动态生成的,然后传递到后端到 C#

var listoftextboxesWithValues = new Array();
var listoftextboxesWithID = new Array();
var i = 0;
$.each(listOftxtPriceTypeID, function (index, value) {
    listoftextboxesWithID[i] = value.ID.toString();
    listoftextboxesWithValues[i] = $("#txtPriceTypeID" + value.ID).val().toString();
    i++;
});

//---Till here the data in the above arrays is as expected, the problem starts below in the data :

$.ajax({
    type: "POST",
    url: "/MemberPages/AdminPages/AdminMainPage.aspx/StoreNewProduct",
    data: "{subCategoryID : '" + parseInt(subcategoryID) + "',name: '" + name + "',description: '" + description + "',quantity: '" + parseInt(quantity) + "',supplier: '" + supplier + "',vatRate: '" + parseFloat(VatRate) + "',colorID: '" + parseInt(colorID) + "',brandID: '" + parseInt(brandID) + "',imagePath: '" + fileNameGUID + "',listOfTextBoxes: '" + JSON.stringify(listoftextboxesWithValues) + "',listOfTextBoxesValues: '" + JSON.stringify(listoftextboxesWithID) + "' }",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert("oh yeh");
    },
    error: function (error) {
        alert("An Error Occured");
    }
}); 



[WebMethod]
    public static void StoreNewProduct(int subCategoryID, string name, string description, int quantity, 
        string supplier, float vatRate, int colorID, int brandID, string imagePath, string[]  listOfTextBoxesID, string[]  listOfTextBoxesValues )
    {
        Product p = new Product();
        ProductPriceType ppt = new ProductPriceType();

        p.CategoryID = subCategoryID;
        p.Name = name;
        p.Description = description;
        p.Quantity = quantity;
        p.Supplier = supplier;
       // p.VATRate = vatRate;
        p.ColorID = colorID;
        p.BrandID = brandID;
        p.Image = imagePath;
        //...
    }

任何帮助将不胜感激

【问题讨论】:

    标签: c# javascript ajax jquery


    【解决方案1】:

    如果我会这样做,我的方法是将两者分开,

    1. 为文本创建字符串数组
    2. 为值创建字符串数组

    我相信值的数量与文本相同,因为它是动态生成的。

    然后在 c# 后端传递这两个字符串数组。

    【讨论】:

      【解决方案2】:

      你可以用json2.js很酷,我用过这个

      var valueObj = { field1: $("input[name=field1]").val(),
                              field2: $("input[name=field2]").val()}
      

      然后我可以用这个解析:

      JSON.stringify(valueObj)
      

      在ajax调用中你可以这样使用

      $.ajax({
          type: "POST",
          url: "/MemberPages/AdminPages/AdminMainPage.aspx/StoreNewProduct",
          data:valueObj ,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
              alert("oh yeh");
          },
          error: function (error) {
              alert("An Error Occured");
          }
      }); 
      

      【讨论】:

      • 但这不是只针对一个对象吗?我如何将它用于多个对象?
      • //我认为您可以像这样发布 2 个对象: data :{object1:valueObj2,object2:valueObj2} //但请检查此答案是否与您想要的类似 stackoverflow.com/questions/1545316/…
      【解决方案3】:
      //In JS file
      var arr = [];
      arr.push( $("#textZipcode").val());
      arr.push( $("#textPhone").val());
      arr.push($("#textAddress").val());
      arr.push( $("#textMobile").val());
      //You can add any number. It will store to array properly.
      $.ajax({
      type: "POST",
      url: "HomePage.aspx/SaveData",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data:JSON.stringify({arr:arr}),
      success: function (response) {
      }});
      
      //In C#
      [WebMethod]
      public static void SaveData(string[] arr)
      {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 2010-12-15
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多