title: Knockout binding not working
body: <p>I have developed a site with MVC4, jQuery and Twitter Boostrap. Now I decided to add knockout to some pages in order to make the workflow more fluent. On this particular page the intention  is to have a list of items to the left that can be reordered with jQuery UI sortable items. When clicked the details of each item will turn up in an area to the right for editing. Depending on type the the editing will be very different so I decided to have separate DIV tags that are made visible depending on type.</p>
  
  <p>The previous working version loads the page items through the model. My idea was to add an observable array to the knockout viewmodel and then just load it on page load. The model is defined on the server so my intention was to not define all properties once again on the client viewmodel. Further manipulation and editing would just use the same collection and upload new/changed/deleted values to the server through AJAX calls.</p>
  
  <p>I have read the knockout demos and searched stackoverflow for similar usecases. I think I followed the suggestions, but obviously something is missing. The model is loaded all right. The items turn up and are displayed with different colors depending on type. They are sortable and on resort the changes are sent back to server and persisted all right.</p>
  
  <p>The missing part is the display and editing of the currently selected item. I added another property named currentItem to the viewmodel. In the function $("body").on("click", ".sortableItem" I can verify that the currentItem is loaded and the Heading property is correct.</p>
  
  <p>But visibility and content of the DIV divEditType1 is not affected. Have I misunderstood the binding concept? Have I misunderstood the loading from server JSON array? Any help is most appreciated. </p>
  
  <p>This is the server page model:</p>
  
  <pre><code>public class PresentationItemsModel
  {
      public Guid PresentationId { get; set; }
      public string DisplayName { get; set; }
      public List&lt;PresentationItemModel&gt; PresentationItems { get; set; }
  }
  </code></pre>
  
  <p>This is the repeated item model:</p>
  
  <pre><code>public class PresentationItemModel
  {
      public Guid PresentationItemId { get; set; }
      public string PresentationItemType { get; set; }
      public int OrderNumber { get; set; }
      public string Heading { get; set; }
      public string Content { get; set; }
      ....
  }
  </code></pre>
  
  <p>This is the client page (irrelevant parts removed):</p>
  
  <pre><code>@model MyCustomWeb.Models.PresentationItemsModel
  
  &lt;header&gt;
      &lt;h2&gt;@Model.DisplayName&lt;/h2&gt;
  &lt;/header&gt;
  
  &lt;div style="float: left;"&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Current list&lt;/legend&gt;
          &lt;ul id="sortable" data-bind="foreach: presentationItems"&gt;
              &lt;li class="sortableItem btn btn-block" data-bind="css: 'sortable' + PresentationItemType + 'Item'"&gt;
                  &lt;div style="font-weight: 500;"&gt;
                      &lt;span data-bind="text: Heading"&gt;&lt;/span&gt;
                      &lt;span class="ui-icon ui-icon-arrowthick-2-n-s" style="display: inline-block; color: gray; float: right; opacity: 0.5; height: 14px; margin: 2px 4px 0 0"&gt;&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  
  &lt;div id="divEditType1" data-bind="visible: currentItem.PresentationItemType == 'Type1'" style="float: left; margin-left: 50px;"&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;Edit Type 1&lt;/legend&gt;
          &lt;div class="control-group"&gt;
              &lt;input type="text" data-bind="value: currentItem.Heading" placeholder = "Enter Heading" /&gt;
          &lt;/div&gt;
          &lt;div class="control-group"&gt;
              &lt;button id="btnSave" type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
              &lt;button id="btnDelete" type="submit" class="btn btn-danger"&gt;Delete&lt;/button&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  
  &lt;div id="divEditType2" data-bind="visible: currentItem.PresentationItemType = 'Type2'" style="float: left; margin-left: 50px;"&gt;
  &lt;/div&gt;
  
  &lt;div id="divEditType3" data-bind="visible: currentItem.PresentationItemType = 'Type3'" style="float: left; margin-left: 50px;"&gt;
  &lt;/div&gt;
  
  
  @section Scripts {
      &lt;script&gt;
          $(document).ready(function () {
  
              // Enable item sorting
              $("#sortable").sortable(
                  {
                      cursor: "move",
                      placeholder: "sortableSeparator",
                      update: function () {
                          // Create array of all changed items
                          var changeItems = [];
                          var currentOrderNumber = 1;
                          $(".sortableItem").each(function () {
                              var id = $(this).attr('data-id');
                              var previousOrderNumber = $(this).attr('data-orderNumber');
                              if (currentOrderNumber != previousOrderNumber)
                                  changeItems.push({ "id": id, "orderNumber": currentOrderNumber });
                              currentOrderNumber += 1;
                          });
                          // Send ajax action
                          $.ajax({
                              type: "PUT",
                              url: "/api/PresentationApi/ChangeOrder/" + "@Model.PresentationId",
                              contentType: 'application/json; charset=utf-8',
                              data: JSON.stringify(changeItems)
                          });
                      },
                  }
              );
  
              // Enable change depending on item click
              // Note: Must be done this way beacuse of the dynamic binding!
              $("body").on("click", ".sortableItem", function () {
                  viewModel.currentItem = ko.dataFor(this);
                  alert(viewModel.currentItem.Heading);  // Works fine!
                  //alert(JSON.stringify(viewModel.currentItem));  // Also looks good!
              });
  
  
  
              // Knockout view model
              function ViewModel() {
                  var self = this;
                  self.presentationItems = ko.observableArray([]);
                  self.currentItem = ko.observable();
              }
  
              var viewModel = new ViewModel();
              viewModel.presentationItems(@Html.Raw(Json.Encode(Model.PresentationItems)));
              ko.applyBindings(viewModel);
          });
      &lt;/script&gt;
  }
  </code></pre>
  
  <p>Edit:
  An attempt to create a Fiddle demo can be found here: 
  <a href="http://jsfiddle.net/XXNz9/" rel="nofollow">http://jsfiddle.net/XXNz9/</a></p>