Saving Knock out js Drop Down value

36 views
Skip to first unread message

ajaykhy...@gmail.com

unread,
Apr 24, 2014, 2:40:08 PM4/24/14
to knock...@googlegroups.com

Hi i want to save the selected value of a drop down to the server , But every time it is returning same value not the updated value which i selected.


My ko:


   

      var stateModel = {
          CountriesList: ko.observableArray([]),
          StateId: ko.observable(),
          StateName: ko.observable('').extend({ required: true }),
          ShortName: ko.observable('').extend({ required: true }),
          IsActive: ko.observable(true),
          CountryId: ko.observable()



      var stateViewModel = function () {
          var self = this;
          self.StateModel = stateModel;
          self.CountriesList = ko.observableArray([]);
          self.StatesList = ko.observableArray([]);
          self.CountryId = ko.observable();

am calling this Add State in a button click, But every time it is returing in args like:
{"argBO":{"CountriesList":[],"StateName":"asdf","ShortName":"s","IsActive":true}}

self.AddState = function () {
var args = JSON.stringify({ argBO: jQuery.parseJSON(ko.toJSON(self.StateModel)) });
alert(args);
ajax.Post("AddState.aspx/AddState", args, false).success(function (data) {
if (data.d[0] > 0) {
logger.success(data.d[1]);
}
else {
logger.error(data.d[1]);
}

});
}

var vm = new stateViewModel();

ajax.Get("AddCountry.aspx/getCountries", { IsActive: true }, false).success(function (data) {
for (var i = 0; i < data.d.length; i++) {

vm.CountriesList.push(data.d[i]);
}



});



ko.applyBindings(vm, document.getElementById("divcountry"));


ko.applyBindings(stateViewModel, document.getElementById("state_container"));

var vm1 = new stateViewModel();
ajax.Get("AddState.aspx/getStates", { IsActive: true }, false).success(function (data) {
vm1.StatesList(data.d);
});

Dropdown is in divcountry, and StateName,ShortName are in state_containerdiv..Actually i have dummy label in divCountry beside DropDown which is giving me correct selected value depending on the Dropdown selection...

<div id="divcountry"> <select data-bind="options: CountriesList,optionsText: 'CountryName',optionsValue:'CountryId',value:CountryId,optionsCaption: 'Select Country..'" style="width: 148px"> </select> <label data-bind="text: CountryId"></label> </div>

Patrick Steele

unread,
Apr 25, 2014, 7:40:51 AM4/25/14
to knock...@googlegroups.com
Could you create a jsFiddle showing the issue?

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages