title: Passing knockout.js observablearray object to MVC Controller Action?
body: <p>Im using knockout with MVC. Im trying to pass an observable array of objects from knockout back to my MVC controller action for saving to the database. If I pass the Array from knockout over to my controller action via ko.toJSON(viewModel.ArrayName) it comes back as null in my controller parameter. If I try to pass it to MVC via ko.toJS(viewModel.ArrayName) it has the correct number of items but the data is null for some reason. Any help on how to do this would be greeatly appreciated. Thanks!</p>
  
  <p><strong>My JQuery Data Retrieval Method:</strong></p>
  
  <pre><code>var dataService = {};
  var viewModel;
  
  $(document).ready(function () {
      dataService.getAccessLevelList();
  })
  
  dataService.getAccessLevelList = function () {
      $.post('/DataService/GetAccessLevelList', null, function (data) {
          viewModel = ko.mapping.fromJS(data); 
          ko.applyBindings(viewModel);
      });
  }
  </code></pre>
  
  <p><strong>This is the problem method:</strong></p>
  
  <pre><code>updateAccessLevels = function () {
      $.post('/DataService/UpdateAccessLevels', { accessLevels: ko.toJSON(viewModel.AccessLevels) }, function (status) {
          alert(status);
      });
  }
  </code></pre>
  
  <p><strong>My MVC Controller Data Retrieval action:</strong></p>
  
  <pre><code>    [HttpPost]
      public ActionResult GetAccessLevelList()
      {
          FacilityAccessViewModel viewModel = new FacilityAccessViewModel();
          viewModel.AccessLevels = unitOfWork.AccessLevelRepository.Get().ToList();
          return Json(viewModel);
      }
  </code></pre>
  
  <p><strong>The parameter is coming back NULL or with NULL data when trying to pass it in from Knockout on this controller method.</strong></p>
  
  <pre><code>    [HttpPost]
      public ActionResult UpdateAccessLevels(List&lt;AccessLevel&gt; accessLevels)
      {
          try
          {
              foreach (AccessLevel record in accessLevels)
              {
                  unitOfWork.AccessLevelRepository.Update(record);
              }
  
              unitOfWork.Save();
              return Json("SUCCESS");
          }
          catch (Exception ex)
          {
              return Json(ex.ToString());
          }
      }
  </code></pre>
  
  <p><strong>Below is the JSON data shown via fiddler thats being posted to my MVC controller action when i pass in { accessLevels: ko.toJSON(viewModel.AccessLevels) } the controller parameter is coming in null using this</strong></p>
  
  <pre><code>[{"Access":[],"Id":1,"AccessLevelDesc":"TEST222"},{"Access":[],"Id":2,"AccessLevelDesc":"TEST222"}]
  </code></pre>
  
  <p><strong>Below is the JS data shown via fiddler thats being posted to my MVC controller action when i pass in { accessLevels: ko.toJS(viewModel.AccessLevels) }, in this case the list has two members as expected, but the data is all null in the properties</strong></p>
  
  <pre><code>accessLevels[0][Id] 1
  accessLevels[0][AccessLevelDesc]    TEST222
  accessLevels[1][Id] 2
  accessLevels[1][AccessLevelDesc]    TEST222
  </code></pre>
  
  <p>If I pass in a single object to my controller it works just fine, but I cant seem to figure out the proper way to post an array of objects to my controller from an obervablearray back to a POCO entity.</p>