Ajax post to ASP.net MVC controller - object properties are null


Back to learning
Created: 25/09/2020

Ajax post to ASP.net MVC controller - object properties are null


Make sure that you pass your array of objects as POST

Controller

        [HttpPost]
        public JsonResult MyMethod(List<xxx> data)
        {
            List<xxx> lstTest = new List<xxx>();

            return Json(lstTest, JsonRequestBehavior.AllowGet);
        }

Js

var data2= [];

 $.ajax({
        url: '/Home/MyMethod',
        type: 'POST',
        data: JSON.stringify({ data: data2}),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        beforeSend: function (xhr) {
            
        }
    }).always(function () {
       
    }).done(function (result) {
        console.log(result)

    }).fail(function (xhr, status, error) {

    });