Why are you declaring ProductOffers
as ko.observable("")
? It should be declared as an observable array instead: ProductOffers: ko.observableArray([]);
Also, in your JFiddle:
function GetProductOffers(ProductID) {
var Val = "[new Offer(1,'Name'),new Offer(2,'Product A'),new Offer(4,'Product B'),new Offer(5,'Product C')]";
return Val;
}
Should be:
function GetProductOffers(ProductID) {
var Val = [new Offer(1,'Name'),new Offer(2,'Product A'),new Offer(4,'Product B'),new Offer(5,'Product C')];
return Val;
}
EDIT:
Try to modify your setup as follows:
[WebMethod]
public static string GetProductOffers(long ProductID)
{
List<DMS.ProductOfferDO> offers = ProductOffers.Get(ProductID);
return JsonConvert.SerializeObject(offers);
}
You need to import: using Newtonsoft.Json;
first of course.
And why did you use post? It should be a get:
function GetProductOffers(ProductID) {
$.get("testing.aspx/GetProductOffers",
{ ProductID: ko.toJSON(ProductID) }
)
.done(function (data) {
viewModel.ProductOffers(JSON.parse(data));
})
.fail(function (data) { })
.always(function () { });
}
EDIT2:
viewModel.ProductID.subscribe(function (newValue) {
viewModel.ProductOffers.removeAll();
if (newValue) {
var productOffers = GetProductOffers(newValue);
viewModel.ProductOffers(productOffers);
}
});
Let us know how this goes please!