我正在尝试Product
在 Javascript 中创建实例,而不是使用[webmethod]
.
[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; }
}
我可以创建但不能使用 Javascript:(有关更多详细信息,请参阅我在代码中的评论Type
)Foo
List<Bar>
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) + "}",
});
}