c# - ASPMVC error navigating to Edit view using knockoutjs -


i using knockout js in navigating view. have list view displays items db table. each row has edit button should forward user edit view, when click it, doesn't display record's properties.

here list view:

<div>     <span data-bind="text: catsearch"></span><br />     <table class="table">         <tr>             <td><b>name:</b></td>             <td><input type="text" class="input-sm" id="txtname" data-bind="value: namesearch"/></td>             <td><b>category:</b></td>             <td><input type="text" class="input-sm" id="txtcategory" data-bind="value: catsearch" /></td>          </tr>         <tr>             <td><button type="button" class="btn btn-primary" data-bind="click: filterlist">search</button></td>         </tr>     </table> </div> <br /> <table class="table ">     <thead>     <tr>         @*<th>id</th>*@         <th class="navbar-header">product</th>         <th>category</th>         <th>price</th>          <th></th>     </tr>     </thead>     <tbody data-bind="foreach: products">         <tr>             @*<td data-bind="text: id"></td>*@             <td data-bind="text: name"></td>             <td data-bind="text: category"></td>             <td data-bind="text: price"></td>              <td>                 <a class="btn btn-default" data-bind="click: editproduct">edit</a>                 <a class="btn btn-danger" data-bind="click: deleteproduct">delete</a>             </td>         </tr>     </tbody> </table>  </div>  @section scripts {     @scripts.render("~/bundles/knockout")     @scripts.render("~/scripts/jquery-3.1.1.min.js")     @scripts.render("~/scripts/knockout-3.4.2.js")     @scripts.render("~/scripts/viewmodels/productlistvm.js") } 

this knockout js getting list:

var urlpath = window.location.pathname; $(function () {     ko.applybindings(productlistvm);     productlistvm.filterlist(); });  //view model var productlistvm = {     products: ko.observablearray([]),      id: ko.observable(0),     name: ko.observable(""),     category: ko.observable(""),     price: ko.observable(0),     statid: ko.observable(0),     namesearch: ko.observable(""),     catsearch: ko.observable(""),     getpass: ko.observable(),     sess: ko.observable(""),        filterlist: function () {         var self = this;         var namep = self.namesearch();         var catp = self.catsearch();           $.ajax({             type: "post",             url: "/product/retrieve",             contenttype: "application/json; charset=utf-8",             datatype: "json",             data: "{\"namesearch\" : \"" + namep + "\" , \"catsearch\" : \"" + catp + "\" }",             success: function (data) {                 self.products(data);             }         });     }  }; //forwarding edit view self.editproduct = function (product) {     window.location.href = '/product/edit/' + product.id; }; self.deleteproduct = function (product) {     window.location.href = '/product/delete/' + product.id; };  //honestly, dont know use of this. doesnt seem have during runtime function products(data) {     this.id = ko.observable(data.id);     this.name = ko.observable(data.name);     this.category = ko.observable(data.category);     this.price = ko.observable(data.price);     this.statid = ko.observable(data.statid); } 

when inspect page after clicked edit button, states:

uncaught referenceerror: unable process binding "value: function (){return id }" message: id not defined     @ value (eval @ parsebindingsstring (knockout-3.4.2.js:68), <anonymous>:3:58) 

you have create new instance of products in ajax success call.

$.ajax({ type: "post", url: "/product/retrieve", contenttype: "application/json; charset=utf-8", datatype: "json", data: "{\"namesearch\" : \"" + namep + "\" , \"catsearch\" : \"" + catp + "\" }", success: function(data) {     var tempproducts = [];     (var = 0; < data.length; i++) {         var productvm = new products(data[i])         tempproducts.push(data);     }     self.products.push.apply(products, tempproducts) }}); 

if don't create new instance of products. in view foreach binding not find properties of productsviewmodel


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -