Reputation: 1527
I use knockout in MVC c# project. I have shopping cart button on my web page and use below code to load shopping cart as modal dialog. This code works for first time but when I click again on shopping card it's not work. I know this problem is related to ko.applyBindings(new CartViewModel()); but I don't how to resolve it,
Any solutions and ideas are welcome.
$("#ShoppingCartButton").on("click", function () {
$("#ShoppingCartDialog").load("/Order/ShoppingCart", function () {
ko.applyBindings(new CartViewModel());
$("#ShoppingCartDialog").modal('show');
});
});
Upvotes: 2
Views: 601
Reputation: 68
You should apply binding only for #ShoppingCartDialog,
ko.applyBindings(new CartViewModel(), "ShoppingCartDialog");
Upvotes: 3
Reputation: 4101
What you could try to do is remove the binding first, and then apply the binding again. You probably also want to bind the viewmodel to just the shoppingCartDialog
:
$("#ShoppingCartButton").on("click", function () {
//remove the binding
ko.cleanNode($("#ShoppingCartDialog")[0]);
$("#ShoppingCartDialog").load("/Order/ShoppingCart", function () {
//bind a new viewModel, just to the shoppingcartdialog.
ko.applyBindings(new CartViewModel(),$("#ShoppingCartDialog")[0]);
$("#ShoppingCartDialog").modal('show');
});
});
Upvotes: 1