Nancy with Complex object using JSON not working
nancy
Solution
Your `BasketRequest` object should implement properties instead of fields. So
public class BasketRequest
{
public User User { get; set; }
public List<Product> Products { get; set; }
}
also you should probably use the generic method too
this.Bind<BasketRequest>();
Problem
Client code: ``` var basket = { products: [], user: { name: "schugh" } }; $("#basket table tr").each(function (index, item) { var product = $(item).data('product'); if (product) { basket.products.push(product); } }); $.ajax({ url: "http://localhost:12116/basketrequest/1", async: true, cache: false, type: 'POST', data: JSON.stringify(basket), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (result) { alert(result); }, error: function (jqXHR, exception) { alert(exception); } }); ``` Server code: ``` Post["/basketrequest/{id}"] = parameters => { var basketRequest = this.Bind(); //basketrequest is null return Response.AsJson(basketRequest , HttpStatusCode.OK); }; ``` Other model classes: ``` [Serializable] public class BasketRequest { public User User; public List<Product> Products; } public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } public ProductStatus ProductStatus { get; set; } } public enum ProductStatus { Created, CheckedBy, Published } public class User { public string Name { get; set; } } ``` The code in the Nancy Module `this.Bind();` returns `null`. If I change the Complex object to just `List<Product>`, i.e. with no wrapper `BasketRequest`, the object is fine... Any pointers? EDIT: JSON posted: ``` { "User": { "Name": "SChugh" }, "Products": [{ "Id": 1, "Name": "Tomato Soup", "Category": "Groceries", "Price": 1 }, { "Id": 2, "Name": "Yo-yo", "Category": "Toys", "Price": 3.75 }] } ```