knockout - pass child array with viewmodel data

WhatsInAName

I have a viewModel like the following:

var viewModel = new function () {

    var self = this;
    self.Id = ko.observable();
     self.currentOrder = {

      orderId: ko.observable(),
      firstCropId: ko.observable(),
      secondCropId: ko.observable(),

      productDataList: ko.observableArray()
  };
       
       
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>

I am trying to pass viewModel.currentOrder to the MVC controller like as follows:

self.SaveOrder = function () {
      var url = '@Url.Action("SaveOrder", "Orders")';
      $.mobile.loading('show');

      var productInfo = {};

      for (i = 0; i < 3; i++) {
        productInfo = { Id: i, Rate: i + 11, Variable: i + 111 };
        viewModel.currentOrder.productDataList.push(productInfo);
      }


       $.ajax({
          type: 'POST',
          url: url,
          dataType: 'json',
          data: viewModel.currentOrder,
          traditional: true,
          success: function (data, status) {

            //success
          },
          error: function (xhr, ajaxOptions, thrownError) {
            $.mobile.loading('hide');
          }

        });

    }; //Save

The controller method looks like this:

public Function SaveOrder(vm As OrdersModels.EditViewModel) As JsonResult

  Dim o As Sales.Order = GetCurrentOrder(vm.OrderId)

  With o
    .FirstCrop = vm.FirstCropId
    .SecondCrop = vm.SecondCropId
    .PreviousCrop = vm.PreviousFirstCropId

    'code to save order
 End function

OrdersModels.EditViewModel looks like this:

Public Class EditViewModel
   Public Property OrderId As Guid
   Public Property FirstCropId As Integer
   Public Property SecondCropId As Integer

   Public Property ProductDataList As List(Of OrderProducts)
End Class

Public Class OrderProducts
  Public Property Id As Integer
  Public Property Rate As Decimal
  Public Property Variable As Decimal
End Class

The controller is getting all data except vm.ProductDataList is nothing. I tried all sorts of things like passing JSON.stringify(viewModel.currentOrder) as data, adding contentType: 'application/json; charset=utf-8' without luck.

How to pass the data to the controller?

Dandy

This should work

$.ajax({
       type: 'POST',
       url: url,
       contentType:'application/json; charset=utf-8',
       dataType: 'json',
       data:ko.toJSON(self.currentOrder),
       success: function (data, status) {
                   //success
       },
       error: function (xhr, ajaxOptions, thrownError) {
          $.mobile.loading('hide');
       }

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

durandal compose - how to pass data in and out of child viewmodel?

From Dev

Passing MVC data to Knockout viewmodel

From Dev

Knockout data-binding to viewModel

From Dev

knockout.js can I pass an an array of data to a template?

From Dev

Knockout associate observable array with another ViewModel

From Dev

Access Knockout ViewModel Data in Sammy route

From Dev

Pass ViewBag data to requirejs or knockout

From Dev

How do I call my viewmodel function from its view - child of viewmodel (knockout.js)

From Dev

Knockout binding handler pass array of objects

From Dev

Knockout Js update ViewModel when data attribute is updated

From Dev

Refreshing ViewModel after deleting data in Knockout.js

From Dev

Knockout Js update ViewModel when data attribute is updated

From Dev

Pass data from code behind and ViewModel to another ViewModel

From Dev

How to pass data between the View and the ViewModel

From Dev

SignalR and Knockout viewmodel binding

From Dev

Knockout Sending ViewModel as parameter

From Dev

Knockout viewModel Property

From Dev

Knockout Sending ViewModel as parameter

From Dev

Update ViewModel in Knockout

From Dev

Knockout data binding a concatenated array of strings

From Dev

Knockout pushing observable and computed data to an observable array

From Dev

How to pass value from one view model to another viewmodel in knockout js?

From Dev

how can I pass dynamically data to a knockout template binding

From Dev

how do I update an array member when using knockout.viewmodel?

From Dev

Pass data from parent to child window

From Dev

How to pass AJAX data to child through props

From Dev

Pass data from parent to child fragment on ViewPagerAdapter

From Dev

How to pass AJAX data to child through props

From Dev

Pass parent data members value to child class

Related Related

  1. 1

    durandal compose - how to pass data in and out of child viewmodel?

  2. 2

    Passing MVC data to Knockout viewmodel

  3. 3

    Knockout data-binding to viewModel

  4. 4

    knockout.js can I pass an an array of data to a template?

  5. 5

    Knockout associate observable array with another ViewModel

  6. 6

    Access Knockout ViewModel Data in Sammy route

  7. 7

    Pass ViewBag data to requirejs or knockout

  8. 8

    How do I call my viewmodel function from its view - child of viewmodel (knockout.js)

  9. 9

    Knockout binding handler pass array of objects

  10. 10

    Knockout Js update ViewModel when data attribute is updated

  11. 11

    Refreshing ViewModel after deleting data in Knockout.js

  12. 12

    Knockout Js update ViewModel when data attribute is updated

  13. 13

    Pass data from code behind and ViewModel to another ViewModel

  14. 14

    How to pass data between the View and the ViewModel

  15. 15

    SignalR and Knockout viewmodel binding

  16. 16

    Knockout Sending ViewModel as parameter

  17. 17

    Knockout viewModel Property

  18. 18

    Knockout Sending ViewModel as parameter

  19. 19

    Update ViewModel in Knockout

  20. 20

    Knockout data binding a concatenated array of strings

  21. 21

    Knockout pushing observable and computed data to an observable array

  22. 22

    How to pass value from one view model to another viewmodel in knockout js?

  23. 23

    how can I pass dynamically data to a knockout template binding

  24. 24

    how do I update an array member when using knockout.viewmodel?

  25. 25

    Pass data from parent to child window

  26. 26

    How to pass AJAX data to child through props

  27. 27

    Pass data from parent to child fragment on ViewPagerAdapter

  28. 28

    How to pass AJAX data to child through props

  29. 29

    Pass parent data members value to child class

HotTag

Archive