title: MVC knockout select tag value assignment
body: <p>I am trying to load a select tag from an ajax call.  I can get the select statement loaded, but when I try to assign the value it fails. Thanks for any help!</p>
  
  <p>HTML Code</p>
  
  <pre><code>&lt;select data-bind="value: state, options: allStates, optionsText: 'name', optionsValue: 'abbreviation', optionsCaption:'Please pick a state...'" class="form-control" id="state" name="state"&gt;&lt;/select&gt;'
  
  var cSource = function (data) {
      var self = this;'
      self.state = ko.observable(data.state);
      self.allStates = ko.observableArray([]);
  
  // get the list of states from the server
  $.getJSON('@Url.Action( "AllStates", "Home" )', function(myData) {
      // myDate is a list of states
      self.allStates(myData);
  });
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ClCfz.jpg" rel="nofollow noreferrer">Not Assigned</a></p>
  
  <pre><code>var modelData = @Html.Raw( Json.Encode( Model ) );  // does not contain a list of states
      mySource = new cSource(modelData); 
      ko.applyBindings(mySource);
  </code></pre>
  
  <p>C# Object</p>
  
  <pre><code>public static List&lt;cStates&gt; usStates()
   {
          List&lt;cStates&gt; myStates = new List&lt;cStates&gt;();
          myStates.Add( new cStates() { abbreviation = "AL", name = "ALABAMA" } );
          myStates.Add( new cStates() { abbreviation = "AK", name = "ALASKA" } );
          ....
          return myStates;
   }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public ActionResult AllStates()
    {
          return Json( ChoiceProducts.States.BLL.cStates.usStates(), JsonRequestBehavior.AllowGet);
    }
  </code></pre>
  
  <p>If I load the states from a javascript json object it works.</p>
  
  <pre><code>var states = [{"name": "Alabama","abbreviation": "AL"},{"name": "Alaska",
      "abbreviation": "AK"},.....
  self.state = ko.observable(data.state);
  self.allStates = ko.observableArray(states);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/wREP3.jpg" rel="nofollow noreferrer">Properly Assigned</a></p>