How to enable data binding in KnockoutJS using the ASP.NET MVC 3 “Razor” View Engine ?

How to enable data binding in KnockoutJS using the ASP.NET MVC 3 “Razor” View Engine ?

Asked on December 17, 2018 in knockout js.
Add Comment


  • 2 Answer(s)

    Using the this method for enable the data binding :

    var initialData = @Html.Raw(Json.Encode(Model));
    
    
    Answered on December 17, 2018.
    Add Comment

        Do not do anything till  the DOM loaded to call ko.applyBindings(viewModel) – so use this code :

    $(document).ready(function () { ko.applyBindings(viewModel); });
    

        This is the full script :

    <script type="text/javascript">
    var initialData = @Html.Raw( new JavaScriptSerializer().Serialize(Model));
    var viewModel = {
        gifts: ko.observableArray(initialData)
    };
    $(document).ready(function () { ko.applyBindings(viewModel); });
    </script>
    

        This worked with knockout-1.2.1.js and jquery-1.5.1.js

    Answered on December 17, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.