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

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

but isnt that for one object only ? how can i use it for multiple objects ?
//I think you can post 2 objects like this: data :{object1:valueObj2,object2:valueObj2} //but check this answer is something similar to you want stackoverflow.com/questions/1545316/…
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)
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.