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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:12:39+00:00 2026-06-17T15:12:39+00:00

I want to save my ViewModel to my SQL server. The standard knockout ko.toJSON(viewModel)

  • 0

I want to save my ViewModel to my SQL server. The standard knockout ko.toJSON(viewModel) keeps giving me an undefined error, so after some digging i found this Question, but still keeps giving me undefined.

Here is the code for my viewmodel, this is just a currency conversion table that gets the Conversion Rate between 2 currencies.

   //ko Event Handler for datepicker.js
    ko.bindingHandlers.datepicker = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            //initialize datepicker with some optional options
            var options = allBindingsAccessor().datepickerOptions || {};
            $(element).datepicker(options).on("changeDate", function (ev) {
                var observable = valueAccessor();
                observable(ev.date);
            });
        },
        update: function (element, valueAccessor) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            $(element).datepicker("setValue", value);
        }
    };


    //Currency Model Definition
    var currency = function (data) {
        var self = this;
        self.DateCreated = ko.observable(formatJsonDate(data.DateCreated));
        self.CurrencyFrom = ko.observable(data.CurrencyFrom);
        self.CurrencyTo = ko.observable(data.CurrencyTo);
        self.ConversionRate = ko.observable(data.ConversionRate);

        //Yhoo Finance API
        ko.computed(function () {
            var from = self.CurrencyFrom(),
                 to = self.CurrencyTo();


            if (!from || !to) {
                self.ConversionRate("N/A");
                return;
            }

            getRate(from, to).done(function (YahooData) {
                console.log("got yahoo data for [" + from + "," + to + "]: ", YahooData);
                self.ConversionRate(YahooData.query.results.row.rate);
            });
        });
    }

    var NewDate = new Date().getFullYear() + '-' + ("0" + (new Date().getMonth() + 1)).slice(-2) + '-' + new Date().getDate();


    var CurrencyModel = function (Currencies) {
        var self = this;
        self.Currencies = ko.observableArray(Currencies);

        self.AddCurrency = function () {
            self.Currencies.push({
                DateCreated: NewDate,
                CurrencyFrom: "",
                CurrencyTo: "",
                ConversionRate: ""
            });
        };

        self.RemoveCurrency = function (Currency) {
            self.Currencies.remove(Currency);
        };

        self.Save = function () {

            $.ajax({
                url: "#",
                contentType: 'application/json',
                data: ko.mapping.toJSON(CurrencyModel),
                type: "POST",
                dataType: 'json',
                success: function (data) {
                    //I get undefined....
                    alert(ko.mapping.toJSON(CurrencyModel));
                },
                error: function (data) { alert("error" + data); }
            });
        }

        $.ajax({
            url: "CurrencyConfiguration.aspx/GetConfiguredCurrencies",
            data: '{}',
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "JSON",
            timeout: 10000,
            success: function (Result) {
                //                    callback(Result);
                var MappedCurrencies =
              $.map(Result.d,
             function (item) {
                 getRate(item.CurrencyFrom, item.CurrencyTo);
                 return new currency(item);
             }
       );
                self.Currencies(MappedCurrencies);

            },
            error: function (xhr, status) {
                alert(status + " - " + xhr.responseText);
            }
        });

    };

    function formatJsonDate(jsonDate) {
        var FormatDate = new Date(parseInt(jsonDate.substr(6)));
        var Output = FormatDate.getFullYear() + '-' + ("0" + (FormatDate.getMonth() + 1)).slice(-2) + '-' + FormatDate.getDate();
        return (Output);
    };

    function getRate(from, to) {
        return $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D" + from + to + "%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=?");
    }

        function callback(data) {
        if (viewModel) {
            // model was initialized, update it
            ko.mapping.fromJS(data, viewModel);
        } else {
            // or initialize and bind
            viewModel = ko.mapping.fromJS(data);
            ko.applyBindings(viewModel);
        }
    }

    $('#Date').datepicker();

    $(document).ready(function () {
        var VM = new CurrencyModel();
        ko.applyBindings(VM);
    })
  • 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-17T15:12:40+00:00Added an answer on June 17, 2026 at 3:12 pm
    data: ko.mapping.toJSON(CurrencyModel)
    

    You are trying to JSONify the definition not the actual instance

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

Sidebar

Related Questions

I want to save a remote img-file to my server, but I don't know
button has a Command binding into ViewModel (it runs some Save method in ViewModel).
I want save the username and password when user login, and I added the
i want to save a variable to be global variable on all screens i
I want to save values as an ArrayList of double in a file. Whenever
I want to save webpage in document directory with the images,css and javascripts etc..
I want to save my program's DataModel objects to a file, and be able
I want to save a local copy of xml being ouput by a certain
I want to save a matrix to a text file, so I can read
I want to save and load my xml data using XmlReader. But I don't

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.