0

The GetAllCountries retrieve list of countries from controller action(MVC).I am unable to assign the countries to select element( as shown in the mark up). On the other hand if I assign values as model.countries = ["India","USA"]; it worked. How do I assign the return values?

var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model));


    function StudentViewModel() {
        var self = this;
        self.students = ko.observableArray([]);
        self.countries =[];
        self.editingItem = ko.observable();
        self.isItemEditing = function(itemToTest) {
            return itemToTest == self.editingItem();
        };
    };
    $(document).ready(function () {
        var url = '/GridDemo/GetAllCountries';
        $.ajax({ url: url, success: dataRetrieved, type: 'POST', dataType: 'json' });
        var model = new StudentViewModel();
        //model.countries = ["India","USA"];
        function dataRetrieved(data) {
            var strCountry = [];
            for (var i = 0; i<data.length; i++) {
                strCountry.push(data[i]);
            }
            //alert(strCountry);
           model.countries = strCountry;

        }
       // alert(initialData.country);
        //model.countries = initialData.Countries;
        model.students(initialData);
        ko.applyBindings(model);   

    });

HTML :

 <select class="form-control" data-bind="options: $root.countries, value: 
  Country, visible: $root.isItemEditing($data)"></select>
 <label data-bind="text: Country, visible: !$root.isItemEditing($data)" />

When I pop up the json result I got exactly: India,America,Nepal.

Action in the controller:

 public JsonResult GetAllCountries()
        {
            var countries = new[] { "India", "America", "Nepal" };
            return Json(countries, JsonRequestBehavior.AllowGet);
        }
3
  • what does the url url = '/GridDemo/GetAllCountries'; return. I cant see the code. Make Sure it return valid json Commented Feb 1, 2014 at 9:34
  • Please check the above changes. Commented Feb 1, 2014 at 9:59
  • But Anyway i was able to parse the result and push into array. Still the assignment of the array doesn't work (please see above) Commented Feb 1, 2014 at 10:03

1 Answer 1

1

You need to make your countries also an ko.observableArray

function StudentViewModel() {
        var self = this;
        self.students = ko.observableArray();
        self.countries = ko.observableArray();
        self.editingItem = ko.observable();
        self.isItemEditing = function(itemToTest) {
            return itemToTest == self.editingItem();
        };
    };

And in your dataRetrieved you need to assign the strCountry to the model.countries:

function dataRetrieved(data) {
    var strCountry = [];
    for (var i = 0; i<data.length; i++) {
        strCountry.push(data[i]);
    }
    model.countries(strCountry);
}

Or you can directly push into your observable array (documentation):

function dataRetrieved(data) {
    model.countries.removeAll();
    for (var i = 0; i<data.length; i++) {
        model.countries.push(data[i]);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried both but it didn't work. I got error: removeAll is not a function
@arjun then please a sample response data what you get in the dataRetrieved function!
You should also check your browser's javascript console looking for errors!
Could you please see the changes above
Please check again your console for errors. If you correctly receives an array in dataRetrived then pusgin into an observable array should work. See in this example: jsfiddle.net/AUmqy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.