Reputation: 645
I have made a jsfiddle with my code at http://jsfiddle.net/dorraba/HuDPJ/ I want to create a simple shopping cart where I can add and remove Items. My intention is that the Product will be an object (Name and Price) The Shopping cart will hold a list of items, where each item is built from a Product and quantity.
My problem is I can't find a way to set the textbox Price by the product selected in the dropdown list in each line.
Any help would be great. Thanks.
Upvotes: 0
Views: 491
Reputation: 93
you should use the value binding for input elements, otherwise you will get a DOM error.
Furthermore you have set the optionsValue
property of the options binding to 'Name'
, which will cause knockout to set the value of ShoppingCartItem.Product
to the name of the product.
Binding to nested properties like Product.Price
will cause an error if a parent (in this case Price
) evaluates to undefined.
Hence you need to test if Product is defined... like this: data-bind="value: (Product() ? Product().Price : '')"
Here is a working solution of your jsfiddle: http://jsfiddle.net/HuDPJ/1/
Upvotes: 2