KnockoutJS : how to set the initial value from a dropdown list when data is retrieved asynchronously using JSON?
-
Issue is related to http://stackoverflow.com/questions/8386316/knockoutjs-initial-values-are-not-posted-to-server-when-using-ko-tojsonthis and http://stackoverflow.com/questions/6753595/set-initial-dropdown-value-to-viewmodel-knockoutjs. The problem is that when a options list is not yet populated with valid data (because the JSON call return is asynchronously), you cannot set the initial selected value. function PersonViewModel() { // Data members this.Function_Id = ko.observable('@(Model.Function_Id)'); this.Functions = ko.observableArray([{ Id: '@(Model.Function_Id)', Name: ''}]); // This works //this.Functions = ko.observableArray(); // This does not work this.SubFunctions = ko.observableArray(); this.GetFunctions = function () { var vm = this; $.getJSON( '@Url.Action("GetFunctions", "Function")', function (data) { vm.Functions(data); if (vm.Function_Id() === undefined) { //vm.Function_Id('@(Model.Function_Id)'); // Only way to solve my problem? } } ); }; } $(document).ready(function () { var personViewModel = new PersonViewModel(); ko.applyBindings(personViewModel); personViewModel.GetFunctions(); }); See this modified http://jsfiddle.net/agjEF/
-
Answer:
Thanks to 'RP Niemeyer' and 'Sandeep G B', I've created http://jsfiddle.net/stefh/vGRLF/. Maybe it's also an idea to add an 'initialValue' property to the data-bind functionality for a the data-bind attribute from a select like this: <select id="Function_Id" data-bind=' options: Functions, optionsValue : "Id", optionsText: "Name", initialValue = 1, value: Function_Id, optionsCaption: Functions().length == 0 ? "Loading..." : "--NONE--"'> </select>
Stef at Stack Overflow Visit the source
Other answers
If the question is still open, here is the answer http://jsfiddle.net/gbsandeep/e3UB3/
Sandeep G B
Related Q & A:
- How to display indexes for UITableView in a complex list?Best solution by User Experience
- How to skip columns empty in CSV file when importing into MySQL table using LOAD DATA INFILE?Best solution by Stack Overflow
- how to set value in ckeditor using jquery?Best solution by Stack Overflow
- How to get the column value when a row is selected in wpf listview?Best solution by Stack Overflow
- How can I remove an element in a repeated list?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.