我試圖在Javascript中創建Product
實例,而不是使用[webmethod]
將它傳遞到服務器。如何在javascript中創建對象的嵌套列表
[WebMethod]
public static void SetProduct(Product product)
{
// i want a product instance
}
以下是Product
類,我想創建:
public class Product
{
public Type Type { get; set; }
public Foo Foo { get; set; }
public List<Bar> Bars { get; set; }
}
public class Type
{
public string ID { get; set; }
}
public class Foo
{
public string ID { get; set; }
public string Color { get; set; }
}
public class Bar
{
public string Name { get; set; }
}
我能夠創建Type
和Foo
但不List<Bar>
在Javascript:(見我的意見的代碼更詳細信息)
的Javascript
function setProduct() {
var product = {};
product.Type = {};
product.Foo = {};
product.Type.ID = 'typeID';
product.Foo.ID = 'fooID';
product.Foo.Color = 'fooColor';
//here is my question how can create List<Bar> Bars and add it to product item???
$.ajax({
type: "POST",
url: "Default.aspx/SetProduct",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
data: "{product:" + JSON.stringify(product) + "}",
});
}
什麼_「我有特魯ble「_是什麼意思?你有什麼問題? – Madbreaks
請在代碼中查看我的意見 – user829174