16

I'm trying to implement this Knockout example using ASP MVC 3's "Razor" view engine.

The first topic covers simple data binding of a C# array using the standard ASP view engine. I am trying the sample example using "Razor", and this line:

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

results in an empty variable for initialData.

I also tried this:

@{
    string data = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model);
}

And then specified the initialData like this:

var initialData = @Html.Raw(data);

This populates initialData with the dataset, but binding does not work.

I'm just trying to databind this set in order to display a count of the ideas, as in the example:

<p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p>

Why isn't data binding working in this instance?

2 Answers 2

24

The easiest way in MVC3 is to do:

var initialData = @Html.Raw(Json.Encode(Model));
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, @RP, but it doesn't work for me. I have this: <p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s).</p> <script type="text/javascript"> var initialData = @Html.Raw(Json.Encode(Model)); var viewModel = { gifts: ko.ObservableArray(initialData) }; ko.applyBindings(viewModel); </script> The bindings don't show up for me.
Can you do a "view source" in your browser and see how initialData looks in the markup? I have a copy of the gift editor sample that works with this code. Did you change anything else? Thanks.
Nothing out of the ordinary. initialData is just a regular JSON array populated with the correct data. I can throw up an alert box with the correct count, but the ko databinding isn't working. It's super frustrating because the live examples are superb.
You can take a look at this: db.tt/5VYfoWz and try to reconcile it with yours.
Just FYI, my sample was breaking because I used the latest jQuery (1.5.1), jQuery template, and Knockout libs. I replaced all of that with @RP's example libs and it worked.
|
11

I ran into this same problem, and discovered that it was my own stupidity that caused the issue (which it so often is). I forgot to wait until the DOM loaded to call ko.applyBindings(viewModel) - so simply using:

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

So the whole script as

<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

1 Comment

You just helped me solve a problem I was banging my head against for the last 2 days. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.