Adding user-defined scripts

If necessary, you can add your own Knockout.js code to the automatically generated code. It is important to initialize the generated model with the following code:

@ko.Initialize(Model)

This method will create a model named viewModel and fill it with data that is taken from the Model parameter. Then, you can add custom JavaScript code and extend a model:

<script type="text/javascript">
  viewModel.someProperty = someValue;  
</script>

After the necessary custom script is added, you should activate Knockout with the following code:

@ko.Apply(Model)

Get Getting Started with Knockout.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.