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

5
  • what is the output of this--> console.log(itemsCart) Commented Dec 27, 2018 at 7:26
  • and missing --> contentType: "application/json; charset=utf-8"` in ajax call? Commented Dec 27, 2018 at 7:28
  • Is the save as the screenshot that I have uploaded Commented Dec 27, 2018 at 7:28
  • link this post says i dont need contentType Commented Dec 27, 2018 at 7:32
  • Two things, does your API work from Postman? Secondly have your tries using [FromBody] in your post action? Commented Dec 27, 2018 at 7:54

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

I have added your code and still the products on controller are null
How about using traditional: true without stringify? Since you want to pass an array directly as viewmodel collection, traditional: true must be set.
And remove the JSON.stringify() ?
Just give a try with data: { products: itemsCart } and set traditional: true because you want to pass viewmodel collection as array.
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.
0

Replace

data: { products: itemsCart }

with

data: ({ products: itemsCart })

Controller:

public IActionResult SuccessView(ProductViewModel products)
  {
    //return View();
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.