javascript - Knockout push new array item using Ajax form -


my application mvc 5 c#, using following form update table:

  @using (ajax.beginform("addoha", "medication", new ajaxoptions() {     updatetargetid = "result",     onsuccess = "getresult",     httpmethod = "post" })) {     <input id="medname" name="medname" value="" class="input-sm form-control" style="width: 100px" placeholder="value"/>     <input type="submit" value="save" class="btn btn-primary"/> } 

here knockout list:

<tbody data-bind="foreach: ohalist">     <tr>         <td>             <input data-bind="value: name" class="input-sm form-control" style="width: 100px" placeholder="value" />         </td>     </tr> </tbody> 

here viewmodel:

var viewmodel = function() {     var self = this;     this.ohalist = ko.observablearray([]),         $.ajax({         type: "get",         url: '@url.action("xxx", "xxx")',         contenttype: "application/json; charset=utf-8",         datatype: "json",         success: function (data) {             self.ohalist(data);         },         error: function (err) {             alert(err.status + " : " + err.statustext);         }     });     var ohamed = {         name: self.name     }; self.ohamed = ko.observable(); }     ko.applybindings(new viewmodel()); 

i tried update list using:

var getresult = function(data) {     if (data[0].name !== "") {         // alert(data[0].name);         viewmodel.ohalist.push({ name: data[0].name });         // viewmodel.ohalist.push({ name: "1});     } else {         alert("failed");     }} 

although results controller, or if code value, following error:

unable property 'push' of undefined or null reference 

would appreciate suggestions.

you need keep reference viewmodel handy, here's 1 way that:

window.viewmodel = new viewmodel(); ko.applybindings(window.viewmodel); 

then in getresult function, need use instance of viewmodel push values to, rather viewmodel type itself:

var getresult = function(data) {     if (data[0].name !== "") {         // alert(data[0].name);         window.viewmodel.ohalist.push({ name: data[0].name });         // viewmodel.ohalist.push({ name: "1});     } else {         alert("failed");     } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -