asp.net mvc 4 - How to update property on KO object for when a radio button is checked/clicked? -


i have read docs on knockoutjs , understand radio button use check bindings. issue me way binding works doesn't fit scenario. in examples, radio button assigned value , when radio button selected, validates value of radio button versus value in ko.computed function determine radio button checked. however, in below example, have radio button per table row.

i need property "isprimary" update true when select radio button row..the name of radio buttons being same prevents multiple selections. have been able property update ko not update property in mvc model post nor check radio button show selected.

 <table data-bind="foreach: details">             <tr>                 <td>                     <div>                         <input data-bind="value: standardaccountno, attr: {name: 'newaccountgroupdetails[' + $index() + '].standardaccountno'}" />                     </div>                 </td>                 <td>                     <div class="radiobutton">                         <input type="radio" data-bind="click: $parent.markprimary, attr: {value: $index()}" name="primary" />                     </div>                 </td>                 <td>                     <a href="#" data-bind="click: $parent.removedetail">remove</a>                 </td>            <tr> </table>   <button data-bind="click: adddetail">new</button> 

knockout

    var detail = function () {     this.standardaccountno = ko.observable(new date());     this.isprimary = ko.observable(false);     this.effectivedate = ko.observable(formatteddate(new date()));     this.enddate = ko.observable(formatteddate(new date())); };  function viewmodel() {     var self = this;     var rawlist = '@html.raw(new system.web.script.serialization.javascriptserializer().serialize(model.newaccountgroupdetails))';     self.details = ko.observablearray(convertjsontokoobservableobject($.parsejson(rawlist)));     self.details.push(new detail());      self.adddetail = function () {         self.details.push(new detail());          $('.datepicker').each(function (i, obj) {             $(obj).datepicker({ changeyear: true, changemonth: true });         });     }      self.markprimary = function () {         console.log(this);         this.isprimary(true);         $('.radiobutton').each(function (i, obj) {             console.log(obj);         });     }      self.removedetail = function () {         self.details.remove(this);     } }  ko.applybindings(new viewmodel()); 

notes: console.logs output ko object represents row selected , works. next 1 outputs each row has radio button on , works. trying use figure out how mark radio button on page checked , ensure property on mvc model gets updated post back.

try this:

 self.markprimary = function (currentdetail) {         ko.utils.arrayforeach(self.details(), function (detail) {             detail.isprimary(false);         });         currentdetail.isprimary(true);         return true;  } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -