2013-12-19 66 views
1

您好我有下面傳遞字符串數組從AJAX到c#web方法

我怎樣才能通過優選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; 
     //... 
    } 

任何幫助,將不勝感激

回答

0

可以使用json2.js很酷,我用這個

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

,然後我可以這樣解析:在阿賈克斯

JSON.stringify(valueObj) 

叫你可以使用這樣

$.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"); 
    } 
}); 
+0

但不是隻有一個對象?我怎樣才能將它用於多個對象? – drinu16

+0

//我認爲你可以發佈這樣的2個對象:data:{object1:valueObj2,object2:valueObj2} //但檢查這個答案是類似於你想要的東西http://stackoverflow.com/questions/1545316/passing-多JSON對象作爲數據-使用-jquerys-AJAX – adiaz

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

,如果我將做到這一點,我的做法是單獨的這兩個,

  1. 爲文本創建字符串數組
  2. 爲值創建字符串數組

我相信值的數量將與文本相同,因爲它是動態生成的。

然後在c#後端傳遞這兩個字符串數組。

相關問題