我试图在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