I'm currently in the process of creating a new Product
instance in Javascript, with the intention of sending it to the server using [webmethod]
.
[WebMethod]
public static void SetProduct(Product product)
{
// I need the Product instance
}
Here is the definition of the Product
class that I am working on:
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; }
}
So far, I have successfully created instances of Type
and Foo
, but I am encountering difficulties with creating List<Bar>
in Javascript (please refer to my comments in the code for more details).
Javascript
function setProduct() {
var product = {};
product.Type = {};
product.Foo = {};
product.Type.ID = 'typeID';
product.Foo.ID = 'fooID';
product.Foo.Color = 'fooColor';
// My question is how can I create a List<Bar> Bars and add it to the product item???
$.ajax({
type: "POST",
url: "Default.aspx/SetProduct",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
data: "{product:" + JSON.stringify(product) + "}",
});
}