Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:25:56+00:00 2026-06-10T02:25:56+00:00

I have the following knockout viewmodel: function DepartmentsViewModel() { var self = this; self.currentComplaint

  • 0

I have the following knockout viewmodel:

function DepartmentsViewModel() {
    var self = this;

    self.currentComplaint = ko.observable('');
    self.departments = ko.observableArray([]);
    self.selectedDepartment = ko.observable('').extend({
        required: true
    });

}

I then populate my departments and assign the selectedDepartment value with a series of remote calls to my Controller (id is just a number I assign to a variable based on the current page the user is at):

$(function() {
    var self = new DepartmentsViewModel();

    self.currentComplaint(id);

    $.ajax({
        url: '@Url.Action("GetDepartments")',
        success: function (data) {
            self.departments(data);
        }
    });

    $.ajax({
        url: '@Url.Action("GetDetails")',
        data: { id: id },
        success: function (data) {
            self.selectedDepartment(data.DepartmentId);
        }
    });

    ko.applyBindings(self);

});

This is then populated in my HTML like so:

<select class="complaint-select" 
        data-bind="options: departments, 
                   optionsText: function(item) { 
                       return item.DepartmentCode + ' - ' + item.DepartmentName 
                   }, 
                   optionsValue: 'DepartmentId',
                   value: selectedDepartment, 
                   optionsCaption: 'Choose..'">
</select>

I notice that sometimes my selectedDepartment doesn’t get set when I refresh my page, and as a result, my dropdown doesn’t have any value set and tells me to select an option from the dropdown? Any idea as to why? I’ve done a console.log(self.selectedDepartment()); on the complete action of my AJAX request and sometimes it is set, but others it is undefined.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:25:58+00:00Added an answer on June 10, 2026 at 2:25 am

    Instead of making separete ajax calls you have to chain these ajax calls to make sure that there is always valid deparmantList exist in your departmantArray for any given id. Because the result’s of ajax calls came randomly you cant be sure which one is come first.

    var departmantLoadProcess =  $.ajax({
            url: '@Url.Action("GetDepartments")'      
    });
    
    departmantLoadProcess.done(function(data){
    
        self.departments(data);
    
        var detailLoadProcess = $.ajax({
            url: '@Url.Action("GetDetails")',
            data: { id: id },
            success: function (data) {         
            }
        });
    
        detailLoadProcess.done(function(data){
           self.selectedDepartment(data.DepartmentId);
        });
    
    });
    

    For more information inspect Jquery deferred object

    EDIT:

    Just like Ryan mentioned serilazing ajax requests cause some overload if we get millions hit 🙂 and we dont need to wait them if their result’s doesnt depend on eachother. We can optimize it further.

    // If both ajax request success
    
    $.when( $.ajax({url: '@Url.Action("GetDepartments")'}),
            $.ajax({url: '@Url.Action("GetDetails")' , data : {id:id}}) ).done(function(deparmantsData,detailsData) {
    
       self.departments(deparmantsData);
       self.selectedDepartment(detailsData.DepartmentId);
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Knockout view model defined like this: function viewModel () { var
I have Knockout.js view model that has nested observable arrays. function ParentVM(data) { var
I have the following knockout.js code I’ve got the following: var itemLine = function()
Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
Lets say I have a viewmodel like this var viewModel = { Dtos: [{
I have the following table template that is rendered through knockout: <table class=gv data-bind=visible:
I have the following view model which contains an array of elements function ReservationsViewModel()
I was following a video about knockout and I have no idea why my
I have following table it shows like this: ABC A B C 123 Hello
How can I do the following validation using knockout js? I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.