skip to Main Content

JS:

newRecords = [{
    "id": 7,
    "name": "Raddish",
    "rate": 30,
    "weight": "2",
    "amountperweight": 60
  },
  {
    "id": 8,
    "name": "Peas",
    "rate": 35,
    "weight": "4",
    "amountperweight": 140
  }
]

$.ajax({
  method: "POST",
  url: "http://localhost:36551/Orders/GenerateOrder",
  data: ({
    "generateOrder": newRecords
  }),
  contentType: "application/json, charset=utf-8",
  success: function(response) {
    console.log("response", response);
  },
  error: function(error) {}
});

Asp.Net:

 public class OrderCart
        {
            public int id { get; set; }
            public string name { get; set; }
            public int rate { get; set; }
            public string weight { get; set; }
            public int amountperweight { get; set; }
        }

    [HttpPost]
    [Route("GenerateOrder")]
    [ActionName("GenerateOrder")]
    public List<OrderCart> GenerateOrder(List<OrderCart> generateOrder)
    {
        return generateOrder;
    }

Postman Post response:
Postman Post response

Ajax Post response in VS:

Ajax Post response in VS

2

Answers


  1. Client-side code will look like this:

    @section scripts{
        <script>
            $(function () {
                var generateOrder = [{ "id": 7, "name": "Raddish", "rate": 30, "weight": "2", "amountperweight": 60 },
                { "id": 8, "name": "Peas", "rate": 35, "weight": "4", "amountperweight": 140 }];
    
                $.ajax({
                    method: "POST",
                    url: "/Orders/GenerateOrder",
                    data: JSON.stringify(generateOrder),
                    contentType: "application/json, charset=utf-8",
                    success: function (response) {
                        console.log("response", response);
                    },
                    error: function (error) {
                    }
                });
            })
        </script>
    }
    

    Server-side action will be:

    [HttpPost]
            public List<OrderCart> GenerateOrder(List<OrderCart> generateOrder)
            {
                return generateOrder;
            }
    

    Now Visual studio should work as expected.

    Login or Signup to reply.
  2. Change the row data in

    data: JSON.stringify([
    { 
        id: 0,
        name: '',
        rate: 0,
        weight: 0,
        amountperweight: 0
    },
    { 
        id: 0,
        name: '',
        rate: 0,
        weight: 0,
        amountperweight: 0
    },
    ...
    ]),
    

    if the variable newRecords is in the format

        [
        { 
            id: 0,
            name: '',
            rate: 0,
            weight: 0,
            amountperweight: 0
        },
        { 
            id: 0,
            name: '',
            rate: 0,
            weight: 0,
            amountperweight: 0
        },
        ...
        ]
    

    do this

    data: JSON.stringify(newRecords),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search