You could do it this way (see fiddle)
Your code wasn't working because in the data-bind="foreach:productname"
list that you had, you were trying to bind to properties of your viewmodel (one instance) instead of properties/observables of the array that you wanted to iterate over. There were also some other things, like data-bind="text:prodname"
yet no prodname
was defined anywhere in your view model. I cleaned it some for you, and hopefully you can adapt this code to meet your needs.
var mymodel = function () {
var self = this;
self.newprod = ko.observable();
self.anzahl = ko.observable();
self.productname = ko.observableArray();
self.addValues = function () {
self.productname.push(new product(self.newprod(), self.anzahl()));
// clear the input boxes for the next entry
self.newprod('');
self.anzahl('');
};
};
function product(name, anz){
var self = this;
self.newproduct = ko.observable(name);
self.anzahl = ko.observable(anz);
}
ko.applyBindings(new mymodel());
and the HTML
<div id="main">
<form data-bind="submit:addValues">
<input type="text" data-bind="value:newprod"></input>
<input type="text" data-bind="value:anzahl"></input>
<button type="submit">OK</button>
</form>
<ul data-bind="foreach:productname">
<li data-bind="text: $index() + 1"></li>
<li data-bind="text: newproduct"></li>
<li data-bind="text: anzahl"></li>
</ul>
</div>