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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:27:20+00:00 2026-05-20T09:27:20+00:00

I have a script that initializes knockout by applying an empty viewmodel to my

  • 0

I have a script that initializes knockout by applying an empty viewmodel to my form.

When the user enters data in the form, the viewmodel is updated accordingly – that is the expected behavior.

JavaScript

var viewModel = {
  myField : ko.observable(),
  init : function (somedata) {
    ...
    ko.applyBindings(this, container);
  },
  ...more stuff...
}

Partial

<%:Html.TextBoxFor(x => x.MyField, new Dictionary<string, object> { { "data-bind", "value: myField, valueUpdate: 'keyup'" } })%>

When i call my javascript to apply knockout in an existing form, the value already in MyField (and then in my input field) is wiped with the empty data in my knockout viewmodel (that viewmodel is part of my javascript residing in another file).

When the page is loaded, MVC makes sure that the MyField input element is filled with the previously entered data from MyField. When init is called, that field is overwritten with the value in the viewmodels myField which is empty, as it’s a static JavaScript file.

How do I solve this problem?

  • 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-05-20T09:27:20+00:00Added an answer on May 20, 2026 at 9:27 am

    I have tried two approaches to handling something like this:

    1- If you have good control over your server-side (which it looks like you do), then you could consider outputting javascript in your view that sets the observables to the proper values.

    Would be somewhat similar to this: http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/. At least this part of it:

    <script type="text/javascript"> 
        var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>; 
    </script>
    

    You could either do that and integrate the initialData before calling applyBindings or possibly just emit the javascript that directly sets your observables to the proper values.

    2- If you don’t have good control over your server-side (a 3rd party control or just something that you are unable/unwilling to change at this point), then you could consider initializing your viewModel after the page has loaded, but before you call applyBindings. You would find the elements that you are interested, read meta-data or values off of them, and set your observables prior to letting the bindings do their work.

    Maybe one complication for you is that sounds like currently all of your javascript is in a separate file.

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

Sidebar

Related Questions

I have a script that needs to extract data temporarily to do extra operations
I have a Bash script that basically initializes an application and sets parameters. One
I have a PHP script that initialises an image gallery. It loops through all
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that takes a table name and generates a control file
I have a script that renders graphs in gnuplot. The graphs all end up
I have a script that works fine on my test server (using IIS6). The
I have a script that successfully encrypts a credit card. I need it to

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.