drinu16
drinu16

Reputation: 795

passing string array from ajax to c# web method

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

Upvotes: 1

Views: 2393

Answers (3)

Francis Saul
Francis Saul

Reputation: 740

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.

Upvotes: 1

Alex Mathew
Alex Mathew

Reputation: 350

//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)
{
}

Upvotes: 0

adiaz
adiaz

Reputation: 1

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");
    }
}); 

Upvotes: 0

Related Questions