1

Hi I have this code below

How could I pass preferably 1 array which will contain an ID number and a value from a textbox which is dynamically generated and then passed to the backend to 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;
        //...
    }

Any help would be much appreciated

drinu16
  • 775
  • 2
  • 11
  • 25

3 Answers3

1

if I will do it, my approach is to seperate those two,

  1. create string array for texts
  2. create string array for values

i believe that the number of values will be the same with texts since it is generated dynamically.

then pass those two string arrays in c# backend.

Jazi
  • 6,569
  • 13
  • 60
  • 92
Francis Saul
  • 728
  • 2
  • 16
  • 37
0

you can use json2.js is cool ,I used this

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

and then I can parse with this:

JSON.stringify(valueObj)

in the ajax call you can use like this

$.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");
    }
}); 
chridam
  • 100,957
  • 23
  • 236
  • 235
adiaz
  • 1
  • 1
  • but isnt that for one object only ? how can i use it for multiple objects ? – drinu16 Dec 19 '13 at 13:54
  • //I think you can post 2 objects like this: data :{object1:valueObj2,object2:valueObj2} //but check this answer is something similar to you want http://stackoverflow.com/questions/1545316/passing-multiple-json-objects-as-data-using-jquerys-ajax – adiaz Dec 19 '13 at 14:01
0
//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)
{
}
Alex Mathew
  • 340
  • 1
  • 3
  • 12