1

I am trying to pass an object using JavaScript to a controller.

I have used two parameters _amount and _total but the values are empty in the controller on postback. Any ideas why I can't get the values? Also if I want to get the _data which is an object containing different values, how will I achieve it?

var itemsCart=toys.cart._items;

$.ajax({
  url: '@Url.Action("SuccessView", "Home")',
  type: 'POST',
  data:  itemsCart,
  success: function (response) {
    alert(response);
  },
  error: function (xhr, ajaxOptions, throwError) {
    alert(xhr.responseText);
  }
});

Controller:

public IActionResult SuccessView(List<ProductViewModel> products)
  {
    return View();
  }

View Model:

public class ProductViewModel
  {
    public string _amount { set; get; }
    public string _total { set; get; }
  }

And a screenshot of the object from the website:

enter image description here

Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31
marios
  • 153
  • 1
  • 14

2 Answers2

1

Sounds like the problem is your parameter is declared as products, but your AJAX data parameter name did not match with controller action parameter name. Try using this setup instead:

JS

$.ajax({
     url: '@Url.Action("SuccessView", "Home")',
     type: 'POST',
     data: { products: itemsCart },
     traditional: true,
     success: function (response) {
         alert(response);
     },
     error: function (xhr, ajaxOptions, throwError) {
         alert(xhr.responseText);
     }
});

Controller Action

[HttpPost]
public IActionResult SuccessView([FromBody] List<ProductViewModel> products)
{
    // do something

    return new JsonResult("OK");
}

Note that since your AJAX call defined as type: POST, you must apply [HttpPost] attribute to the controller action.

Related issue:

Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I have added your code and still the products on controller are null – marios Dec 27 '18 at 07:37
  • How about using `traditional: true` without stringify? Since you want to pass an array directly as viewmodel collection, `traditional: true` must be set. – Tetsuya Yamamoto Dec 27 '18 at 07:40
  • And remove the JSON.stringify() ? – marios Dec 27 '18 at 07:41
  • Just give a try with `data: { products: itemsCart }` and set `traditional: true` because you want to pass viewmodel collection as array. – Tetsuya Yamamoto Dec 27 '18 at 07:42
  • 1
    Try replicate the properties contained inside `toys.cart._items` array into `ProductViewModel` and see if it passing the value, or create new array which only contains 2 required properties. As far as I know, to pass JS array into controller action parameter you need to match amount of properties in both sides. – Tetsuya Yamamoto Dec 27 '18 at 07:54
0

Replace

data: { products: itemsCart }

with

data: ({ products: itemsCart })

Controller:

public IActionResult SuccessView(ProductViewModel products)
  {
    //return View();
  }
LundinCast
  • 9,412
  • 4
  • 36
  • 48
Anoos
  • 186
  • 7