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

  • Home
  • SEARCH
  • 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 8207355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:58:09+00:00 2026-06-07T08:58:09+00:00

Using Knockout 2.0 and MVC3 Razor forms, I am not able to make a

  • 0

Using Knockout 2.0 and MVC3 Razor forms, I am not able to make a dependent observable work when I introduce an ajax method. I have set up a set of observables that are a part of a calculation, and when I return the product of those observables, I am able to set a SPAN tag with the correct result. However, when I try to use the ajax method to handle those observables and return a result, I get unpredictable behavior. First, it appears the ajax POST does not pick up one of the observables when the INPUT fields are updated (var b POSTs to the action method as 0, but then is eventually updated), and then it seems that I am not able to set the result even when it evaluates correctly. It appears there is timing issue with either the observables or the ajax call. Although simply keeping performing the calculation in javascript works fine, my intent is to call ajax methods for more complicated logic. I have removed the call to ko.applybindings from doc.ready(), and also moved the SCRIPT methods to the bottom of the page- this was the only way I found to make this partly functional. My viewModel is set up as follows:

var viewModel = {
    a: ko.observable(0),
    b: ko.observable(1),
    c: ko.observable(2),
    // commented this out, since
    // the dependent observable will handle this
    // d: ko.observable(0)
};

In my dependent observable:

viewModel.d = ko.dependentObservable(function () {
    var theResult = 0;
    $('.theLabel').css("visibility", "visible");
    theResult=viewModel.a() * viewModel.b() * viewModel.c();
    // if we return here we get a valid result 
    return (theResult);

    // prefer to call ajax method
    // first check to ensure one variable is set
    if (viewModel.a() > 0) {
            $.ajax("/myCalculation/getResult", {
                data: ko.toJSON(viewModel),
                type: "post",
                context: viewModel,
                contentType: "application/json",
                success: function (result) {
                    // can't set visibility here
                    $('.theLabel').css("visibility", "visible");
                    // the POST does not pick up some observables, or
                    // does not the set dependent observable at all
                    return result;
                }
            });

        }
    });
  • 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-07T08:58:10+00:00Added an answer on June 7, 2026 at 8:58 am

    There is quite a bit wrong with the function you have setup.

    1.) You are returning from your function before making your AJAX call. The code after your return statement will never execute.

    2.) Even if you omit the first return statement, your AJAX call is Asynchronous… which means it will execute in the background and return control to the outer scope immediately. Since you don’t have a return statement, then you are going to return undefined.

    3.) The comment in your success callback suggests you are expecting the return statement to propagate all the way up to your computed observable. THAT return statement is only scoped to the callback, and not the outer observable. The return value will be used by jQuery, and your observable will long since have returned.

    If you want an observable to call an AJAX function you need a seperate value to store the results of the asynchronous call.

    Here is a simplified example:

    var viewModel = function(){
       var $this = this;
    
       $this.a = ko.observable();
       $this.b = ko.observable();
       $this.results = ko.observable();
    
       //No need to assign this computed observable to a variable
       // because the results will be stored in '$this.results'
       // we just need this to handle the automatic updates
       ko.computed(function(){
          var data = {
             a: $this.a(),
             b: $this.b()
          };
    
          $.post("/do/some/stuff", data, function(results){
             $this.results(results);
          });
       });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 3 view using Razor and Knockout.js that is not
I am using knockout and have an array Properties and each Property can have
I just started using knockout.js and it works great with normal bidings. I have
I am using Knockout Js for my view page. I have a requirement where
I am using Knockout and the Knockout Mapping plugin. My MVC3 Action returns a
I'm working with a new application at work that we're building out using Knockout.js
I have a form which is rendered using html.RenderAction in MVC3. Outside of this
I'm using Knockout with jQuery and jQuery templates. Assume that I have a template
I'm writing a little single page app (using knockout.js :-)) whereby i have a
I have: jquery1.6.min, jquery-tmpl.js (latest beta) and knockout-1.2.0.js. I am using a very simple

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.