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 8785447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:21:48+00:00 2026-06-13T21:21:48+00:00

Here is my ajax call: var totalCost = 0; function GetTotalCost(start, end, rID) {

  • 0

Here is my ajax call:

  var totalCost = 0;
    function GetTotalCost(start, end, rID) 
    {
        $.ajax({
            url: '@Url.Action("CalculateTotalcost")',
            type: 'POST',
            data: JSON.stringify({ start:start, end:end, rID:rID}),
            dataType: 'json',
            processdata: false,
            contentType: 'application/json; charset=utf-8',
            success: function (data) { totalCost = data; }
//            error: function (xhr, ajaxOptions, thrownError) { $('.datepicker1').datepicker("hide"); },
//            complete: function (x, y) { $('.datepicker1').datepicker("refresh"); }
        });
    }

Here is my function from which I call ajax:

 $('.datepicker2').datepicker({
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            yearRange: '2012:2100',
            beforeShowDay: function (date) {
                var day = date.getDate();
                if (day in alreadyTakenDays) {
                    return [false, '', alreadyTakenDays[day]];
                }
                else return [true, 'IsActive'];
            },
            onChangeMonthYear: function (year, month, inst) {
                alreadyTakenDays = {};
                getEvents(month, year);
            },

            onSelect: function (dateText, inst) {
                var end = dateText.substring(0, 2);
                console.log(end);
                var rID = $('#RoomID').val();
                console.log(rID);
                var startingHole = $('#DateOne').val();
                var start = startingHole.substring(0, 2);
                console.log(start);
                GetTotalCost(start, end, rID);

                document.getElementById('TotalCost').value = totalCost.toFixed(2);

            }
        });

After jQuery script is executed I always get 0 for totalCount?
Why is this happening? What should I Do?
I would like to appoint totalCount to Html.TextBoxFor in ASP.NET MVC 3, that is why I need totalCount. Please help.

  • 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-13T21:21:52+00:00Added an answer on June 13, 2026 at 9:21 pm

    You should have success function handler like this to get correct total cost value:

    function GetTotalCost(start, end, rID) 
        {
            $.ajax({
                url: '@Url.Action("CalculateTotalcost")',
                type: 'POST',
                data: JSON.stringify({ start:start, end:end, rID:rID}),
                dataType: 'json',
                processdata: false,
                contentType: 'application/json; charset=utf-8',
                success: function (data) { totalCost = data;
                                           document.getElementById('TotalCost').value = totalCost.toFixed(2); 
                                         }
            });
        }
    

    In code like this:

    GetTotalCost(start, end, rID); 
    document.getElementById('TotalCost').value = totalCost.toFixed(2);
    

    First line cause ajax call, but it does not wait for response and second line is executed immediately. Later, after server responded, success callback function is executed. And it will be executed after second line. That is why your total value is wrong – correct value is set to totalCost after you update TotalCost input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my ajax call: $.ajax({ url: 'url-to-json', type: 'POST', dataType: 'json', cache: 'false', data:
Here is my AJAX call: $.ajax({ type: 'POST', url: '<?php echo site_url('channel_partners/cms/get_news'); ?>', data:
Here is my AJAX call: $(document).ready(function () { $(#btnSignup).click(function () { $.ajax({ type: POST,
I have this ajax call at the moment: $.ajax({ url: misc/sendPM.php, type: POST, data:
Here is how I am submitting my ajax request : $.ajax({ url: myurl, type:
I have a strange error within my $.ajax call here: //CHECK USERNAME $('#username').focusout(function() {
I use this AJAX to call my Excel Export action on the controller: $(#ExportToExcel).click(function
I am doing an AJAX call that returns some HTML and jQuery: <div id='selected'>Here
I am getting an error in an ajax call from jQuery. Here is my
Here is my Ajax code: var myJSONObject = {bindings: [ {ircEvent: PRIVMSG, method: newURI,

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.