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

Refreshing ViewModel after deleting data in Knockout.js

From Dev

How to pass data between the View and the ViewModel

From Dev

SignalR and Knockout viewmodel binding

From Dev

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

From Dev

Passing MVC data to Knockout viewmodel

From Dev

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

From Dev

Knockout Js update ViewModel when data attribute is updated

From Dev

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

From Dev

Knockout Sending ViewModel as parameter

From Dev

Knockout binding handler pass array of objects

From Dev

Pass data from parent to child window

From Dev

How to pass AJAX data to child through props

From Dev

Access Knockout ViewModel Data in Sammy route

From Dev

how can I pass dynamically data to a knockout template binding

From Dev

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

From Dev

Knockout associate observable array with another ViewModel

From Dev

Knockout viewModel Property

From Dev

Knockout Js update ViewModel when data attribute is updated

From Dev

Knockout Sending ViewModel as parameter

From Dev

Pass ViewBag data to requirejs or knockout

From Dev

Pass data from parent to child fragment on ViewPagerAdapter

From Dev

Pass data from code behind and ViewModel to another ViewModel

From Dev

How to pass AJAX data to child through props

From Dev

Knockout data binding a concatenated array of strings

From Dev

Pass parent data members value to child class

From Dev

Knockout pushing observable and computed data to an observable array

From Dev

Knockout data-binding to viewModel

From Dev

Update ViewModel in Knockout

From Dev

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

Related Related

  1. 1

    Refreshing ViewModel after deleting data in Knockout.js

  2. 2

    How to pass data between the View and the ViewModel

  3. 3

    SignalR and Knockout viewmodel binding

  4. 4

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

  5. 5

    Passing MVC data to Knockout viewmodel

  6. 6

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

  7. 7

    Knockout Js update ViewModel when data attribute is updated

  8. 8

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

  9. 9

    Knockout Sending ViewModel as parameter

  10. 10

    Knockout binding handler pass array of objects

  11. 11

    Pass data from parent to child window

  12. 12

    How to pass AJAX data to child through props

  13. 13

    Access Knockout ViewModel Data in Sammy route

  14. 14

    how can I pass dynamically data to a knockout template binding

  15. 15

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

  16. 16

    Knockout associate observable array with another ViewModel

  17. 17

    Knockout viewModel Property

  18. 18

    Knockout Js update ViewModel when data attribute is updated

  19. 19

    Knockout Sending ViewModel as parameter

  20. 20

    Pass ViewBag data to requirejs or knockout

  21. 21

    Pass data from parent to child fragment on ViewPagerAdapter

  22. 22

    Pass data from code behind and ViewModel to another ViewModel

  23. 23

    How to pass AJAX data to child through props

  24. 24

    Knockout data binding a concatenated array of strings

  25. 25

    Pass parent data members value to child class

  26. 26

    Knockout pushing observable and computed data to an observable array

  27. 27

    Knockout data-binding to viewModel

  28. 28

    Update ViewModel in Knockout

  29. 29

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

HotTag

Archive