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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:48:29+00:00 2026-06-06T14:48:29+00:00

So if your Controller Action returns a Model with pre-populated values, how do you

  • 0

So if your Controller Action returns a Model with pre-populated values, how do you make KnockoutJS aware of them?

E.g.:

@Html.TextBoxFor(m => m.Title, new { data_bind="value: title"} )

however, on $(document).ready() where I bind knockout.js ViewModel, this value isn’t yet populated:

$(document).ready({
  var viewModel = {
    title: ko.observable($("#Title").val()) // too early for this?!
  }

  ko.applyBindings(viewModel);​
});

How do you make KnockoutJS work with MVC’s model binding?
One workaround I found was to set the JavaScript variable in my Razor View, like so:

<script>
  var strTitle = '@Model.Title';
</script>

and than use it in the Knockout model binding. That works, but I hate it. What if your form has like hundreds of fields? You don’t want as many JavaScript variables in your page.
Am I missing the obvious here?

  • 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-06T14:48:30+00:00Added an answer on June 6, 2026 at 2:48 pm

    This seems similar to this question. Normally you would set your view model by converting @Model to JSON in a script:

    <script type="text/javascript">
    var model = @(new HtmlString(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model)));
    </script>
    

    You could also create your own binding handler that will initially load the view model based on control values. This new myvalue handler basically calls the existing value handler, except it updates the view model from the initial control value on init.

    ko.bindingHandlers['myvalue'] = {
        'init': function (element, valueAccessor, allBindingsAccessor) {
            // call existing value init code
            ko.bindingHandlers['value'].init(element, valueAccessor, allBindingsAccessor);
    
            // valueUpdateHandler() code
            var modelValue = valueAccessor();
            var elementValue = ko.selectExtensions.readValue(element);
            modelValue(elementValue); // simplified next line, writeValueToProperty isn't exported
            //ko.jsonExpressionRewriting.writeValueToProperty(modelValue, allBindingsAccessor, 'value', elementValue, /* checkIfDifferent: */ true);
        },
        'update': function (element, valueAccessor) {
            // call existing value update code
            ko.bindingHandlers['value'].update(element, valueAccessor);
        }
    };
    

    Then when you call ko.applyBindings, your observable will be set based on the control’s value initially:

    <input type="text" data-bind="myvalue: Title" value="This Title will be used" />
    <input type="text" data-bind="value: Title" value="This will not be used" />
    <!-- MVC -->
    @Html.TextBoxFor(m => m.Title, new { data_bind="myvalue: Title"} )
    

    SAMPLE FIDDLE

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

Sidebar

Related Questions

If your controller action looks like this: respond_to do |format| format.html { raise 'Unsupported'
What's the pros and cons of using a Model-View-Controller model in building your application?
In code igniter, you can connect database and make queries in controller like: $this->db->query(Your
I'm having issues posting a model to a controller action using the AJAX helper.
I have a controller / action in an ASP.NET MVC3 app. It handles the
I have an MVC controller that has this Action Method: [HttpPost] public ActionResult SubmitAction()
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
In Ruby on Rails you can write a simple controller action such as: def
my controller action: [HttpPost] public ActionResult AddPointAndCopyOtherSongToPlaylist(int id) { if (CheckIfAddPointToSelf(User.Identity.Name, id)) { var
there seems to be another problem ..not with JQuery code but with Action controller

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.