131

I'm trying to pass an array of objects into an MVC controller method using jQuery's ajax() function. When I get into the PassThing() C# controller method, the argument "things" is null. I've tried this using a type of List for the argument, but that doesn't work either. What am I doing wrong?

<script type="text/javascript">
    $(document).ready(function () {
        var things = [
            { id: 1, color: 'yellow' },
            { id: 2, color: 'blue' },
            { id: 3, color: 'red' }
        ];

        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: '/Xhr/ThingController/PassThing',
            data: JSON.stringify(things)
        });
    });
</script>

public class ThingController : Controller
{
    public void PassThing(Thing[] things)
    {
        // do stuff with things here...
    }

    public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Halcyon
  • 14,631
  • 17
  • 68
  • 99
  • You can try [JavaScriptSerializer.Deserialize Method (String, Type)](http://msdn.microsoft.com/en-us/library/ee191864.aspx) – jk. Nov 06 '12 at 00:21
  • 3
    Your data is a string, yet your method accepts an array. Change your method to accept a string, then deserialize it within the method. – Bob Horn Nov 06 '12 at 00:36
  • 2
    Your code is correct. I tested it and it worked using MVC 4. Please provide more data to figure it out. – Diego Nov 06 '12 at 01:47
  • This is great stuff but what if you need not just a list of strings to pass but need to include a separate id associated with the list of strings? So like, group id, list of groups under group id. – Nathan McKaskle Aug 16 '17 at 15:26

18 Answers18

213

Using NickW's suggestion, I was able to get this working using things = JSON.stringify({ 'things': things }); Here is the complete code.

$(document).ready(function () {
    var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
    ];      
    
    things = JSON.stringify({ 'things': things });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Home/PassThings',
        data: things,
        success: function () {          
            $('#result').html('"PassThings()" successfully called.');
        },
        failure: function (response) {          
            $('#result').html(response);
        }
    }); 
});


public void PassThings(List<Thing> things)
{
    var t = things;
}

public class Thing
{
    public int Id { get; set; }
    public string Color { get; set; }
}

There are two things I learned from this:

  1. The contentType and dataType settings are absolutely necessary in the ajax() function. It won't work if they are missing. I found this out after much trial and error.

  2. To pass in an array of objects to an MVC controller method, simply use the JSON.stringify({ 'things': things }) format.

starball
  • 20,030
  • 7
  • 43
  • 238
Halcyon
  • 14,631
  • 17
  • 68
  • 99
  • 8
    I was having the same problem and adding the contentType fixed it. Thanks! – Rochelle C Mar 19 '13 at 14:26
  • 10
    Two things to note: JSON.stringify and specifying 'contentType'. – dinesh ygv Nov 07 '14 at 05:40
  • Crud. Still not working for me. my request URL is `http://localhost:52459/Sales/completeSale?itemsInCart=[{"ItemId":1,"Quantity":"1","Price":3.5}]` and `Sales.completeSale` is `public ActionResult completeSale(ItemInCart[] itemsInCart)`, annotated as a `HttpGet`. – abalter Jun 29 '15 at 17:09
  • Apparently type:Post is required too! Data doesn't go through to a Controller otherwise. – Vadims Samsinovs Jul 21 '15 at 07:52
  • 3
    for whatever reason I had to to just use `data: JSON.stringify(things),` – Rob Scott Aug 05 '15 at 02:33
  • I am looking for something similar. Can you let me know how to do.. http://stackoverflow.com/questions/36347558/c-sharp-mvc-no-submit-pass-object-between-views – Ziggler Apr 01 '16 at 16:33
  • I was using the same approach to pass list to controller. However it was working since I was missing "contentType" in ajax call. Thanks @Halcyon to provide complete example. – Tejas Sutar May 04 '16 at 12:58
  • 1
    `dataType` is not necessary. If its omitted, the ajax function will work it out based on the return data –  Aug 04 '17 at 08:54
  • Surprised that you couldn't drop `dataType` and `stringify` similar to the newer [answer from lanternmarsh](https://stackoverflow.com/a/29283863/1028230). Also, word to wise , **make sure your string values aren't `null`s in JavaScript-land**. That threw me for a long loop, though I don't think it's the first time. (◔_◔) – ruffin Feb 22 '18 at 21:43
  • 1
    For aspnetcore 2, remember to add [FromBody] in your controller function otherwise it won't work. – Hoan Dang May 27 '18 at 07:25
  • I had everything same except I used data: JSON.stringify(things) because things = JSON.stringify({ 'things': things }); did not work for me. – mcfred Sep 20 '18 at 11:55
  • this works on **WEB API** with little changes. thanks. – j1rjacob Feb 09 '19 at 16:14
  • my issue.. https://stackoverflow.com/questions/57085268/asp-net-core-2-1-mvc-send-data-from-javascript-to-action-method-using-xmlhttpreq – Ziggler Jul 17 '19 at 23:50
  • vadims already mentioned this, but make sure you use type:Post – josgall Apr 19 '20 at 05:42
  • Be very careful not to initialise your variables in your controller. So public int Id { get; set; } will work, but public int id = 0 won't. Just wasted an hour or so tracking this down! – Andy Brown Nov 19 '20 at 17:36
45

Couldn't you just do this?

var things = [
    { id: 1, color: 'yellow' },
    { id: 2, color: 'blue' },
    { id: 3, color: 'red' }
];
$.post('@Url.Action("PassThings")', { things: things },
   function () {
        $('#result').html('"PassThings()" successfully called.');
   });

...and mark your action with

[HttpPost]
public void PassThings(IEnumerable<Thing> things)
{
    // do stuff with things here...
}
lanternmarsh
  • 683
  • 6
  • 8
  • 4
    This should be the best answer. The JSON.stringify should not be used in this case –  Apr 25 '16 at 09:52
  • This is not working for me..I am using [HttpPost] public int SaveResults(List model) {} and $.post("@Url.Action("SaveResults", "Maps")", {model: dataItems}, function (result) { }); – Samra Jul 07 '17 at 05:46
  • 2
    It worked for me. Absolutely the best answer. I don't know why the Halcyon implementation didn't work. The PassThings function was invoked but the 'things' input variable was empty even if it was filled in the javascript just before the call. – Leonardo Daga Sep 03 '17 at 22:46
  • can we add file upload to it. – Heemanshu Bhalla Apr 17 '23 at 11:42
25

I am using a .Net Core 2.1 Web Application and could not get a single answer here to work. I either got a blank parameter (if the method was called at all) or a 500 server error. I started playing with every possible combination of answers and finally got a working result.

In my case the solution was as follows:

Script - stringify the original array (without using a named property)

    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: mycontrolleraction,
        data: JSON.stringify(things)
    });

And in the controller method, use [FromBody]

    [HttpPost]
    public IActionResult NewBranch([FromBody]IEnumerable<Thing> things)
    {
        return Ok();
    }

Failures include:

  • Naming the content

    data: { content: nodes }, // Server error 500

  • Not having the contentType = Server error 500

Notes

  • dataType is not needed, despite what some answers say, as that is used for the response decoding (so not relevant to the request examples here).
  • List<Thing> also works in the controller method
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
14

Formatting your data that may be the problem. Try either of these:

data: '{ "things":' + JSON.stringify(things) + '}',

Or (from How can I post an array of string to ASP.NET MVC Controller without a form?)

var postData = { things: things };
...
data = postData
Community
  • 1
  • 1
nick_w
  • 14,758
  • 3
  • 51
  • 71
  • Your code is close, but it doesn't work. I was able to get the code working thanks to your suggestion. See my answer above. – Halcyon Nov 06 '12 at 16:39
10

I have perfect answer for all this : I tried so many solution not able to get finally myself able to manage , please find detail answer below:

       $.ajax({
            traditional: true,
            url: "/Conroller/MethodTest",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify( 
               [
                { id: 1, color: 'yellow' },
                { id: 2, color: 'blue' },
                { id: 3, color: 'red' }
                ]),
            success: function (data) {
                $scope.DisplayError(data.requestStatus);
            }
        });

Controler

public class Thing
{
    public int id { get; set; }
    public string color { get; set; }
}

public JsonResult MethodTest(IEnumerable<Thing> datav)
    {
   //now  datav is having all your values
  }
Veera Induvasi
  • 822
  • 7
  • 19
  • You should have more upvotes: traditional: true is the recommended way on the Jquery website – DFTR Dec 16 '19 at 08:02
7

The only way I could get this to work is to pass the JSON as a string and then deserialise it using JavaScriptSerializer.Deserialize<T>(string input), which is pretty strange if that's the default deserializer for MVC 4.

My model has nested lists of objects and the best I could get using JSON data is the uppermost list to have the correct number of items in it, but all the fields in the items were null.

This kind of thing should not be so hard.

    $.ajax({
        type: 'POST',
        url: '/Agri/Map/SaveSelfValuation',
        data: { json: JSON.stringify(model) },
        dataType: 'text',
        success: function (data) {

    [HttpPost]
    public JsonResult DoSomething(string json)
    {
        var model = new JavaScriptSerializer().Deserialize<Valuation>(json);
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
more urgent jest
  • 362
  • 4
  • 11
5

This is how it works fine to me:

var things = [
    { id: 1, color: 'yellow' },
    { id: 2, color: 'blue' },
    { id: 3, color: 'red' }
];

$.ajax({
    ContentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '/Controller/action',
    data: { "things": things },
    success: function () {
        $('#result').html('"PassThings()" successfully called.');
    },
    error: function (response) {
        $('#result').html(response);
    }
});

With "ContentType" in capital "C".

Dinesh Patil
  • 615
  • 2
  • 15
  • 37
Myxomatosis
  • 51
  • 1
  • 3
4

This is working code for your query,you can use it.

Controler

    [HttpPost]
    public ActionResult save(List<ListName> listObject)
    {
    //operation return
    Json(new { istObject }, JsonRequestBehavior.AllowGet); }
    }

javascript

  $("#btnSubmit").click(function () {
    var myColumnDefs = [];
    $('input[type=checkbox]').each(function () {
        if (this.checked) {
            myColumnDefs.push({ 'Status': true, 'ID': $(this).data('id') })
        } else {
            myColumnDefs.push({ 'Status': false, 'ID': $(this).data('id') })
        }
    });
   var data1 = { 'listObject': myColumnDefs};
   var data = JSON.stringify(data1)
   $.ajax({
   type: 'post',
   url: '/Controller/action',
   data:data ,
   contentType: 'application/json; charset=utf-8',
   success: function (response) {
    //do your actions
   },
   error: function (response) {
    alert("error occured");
   }
   });
sach4all
  • 51
  • 4
3

I can confirm that on asp.net core 2.1, removing the content type made my ajax call work.

function PostData() {
    var answer = [];

    for (let i = 0; i < @questionCount; i++) {
        answer[i] = $(`#FeedbackAnswer${i}`).dxForm("instance").option("formData");
    }

    var answerList = { answers: answer }

    $.ajax({
        type: "POST",
        url: "/FeedbackUserAnswer/SubmitForm",
        data: answerList ,
        dataType: 'json',
        error: function (xhr, status, error) { },
        success: function (response) { }
    });
}
[HttpPost]
public IActionResult SubmitForm(List<Feedback_Question> answers)
{}
Josef
  • 2,869
  • 2
  • 22
  • 23
MrSpyretos
  • 51
  • 1
  • 2
2

Wrapping your list of objects with another object containing a property that matches the name of the parameter which is expected by the MVC controller works. The important bit being the wrapper around the object list.

$(document).ready(function () {
    var employeeList = [
        { id: 1, name: 'Bob' },
        { id: 2, name: 'John' },
        { id: 3, name: 'Tom' }
    ];      

    var Employees = {
      EmployeeList: employeeList
    }

    $.ajax({
        dataType: 'json',
        type: 'POST',
        url: '/Employees/Process',
        data: Employees,
        success: function () {          
            $('#InfoPanel').html('It worked!');
        },
        failure: function (response) {          
            $('#InfoPanel').html(response);
        }
    }); 
});


public void Process(List<Employee> EmployeeList)
{
    var emps = EmployeeList;
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Hoodlum
  • 1,443
  • 1
  • 13
  • 24
1
     var List = @Html.Raw(Json.Encode(Model));
$.ajax({
    type: 'post',
    url: '/Controller/action',
    data:JSON.stringify({ 'item': List}),
    contentType: 'application/json; charset=utf-8',
    success: function (response) {
        //do your actions
    },
    error: function (response) {
        alert("error occured");
    }
});
1

Removing contentType just worked for me in asp.net core 3.1

All other methods failed

Basharat Hussain
  • 209
  • 1
  • 3
  • 8
0

If you are using ASP.NET Web API then you should just pass data: JSON.stringify(things).

And your controller should look something like this:

public class PassThingsController : ApiController
{
    public HttpResponseMessage Post(List<Thing> things)
    {
        // code
    }
}
FleGMan
  • 56
  • 1
  • 5
0

Modification from @veeresh i

 var data=[

                        { id: 1, color: 'yellow' },
                        { id: 2, color: 'blue' },
                        { id: 3, color: 'red' }
                        ]; //parameter
        var para={};
        para.datav=data;   //datav from View


        $.ajax({
                    traditional: true,
                    url: "/Conroller/MethodTest",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data:para,
                    success: function (data) {
                        $scope.DisplayError(data.requestStatus);
                    }
                });

In MVC



public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }

    public JsonResult MethodTest(IEnumerable<Thing> datav)
        {
       //now  datav is having all your values
      }
0

What I did when trying to send some data from several selected rows in DataTable to MVC action:

HTML At the beginning of a page:

@Html.AntiForgeryToken()

(just a row is shown, bind from model):

 @foreach (var item in Model.ListOrderLines)
                {
                    <tr data-orderid="@item.OrderId" data-orderlineid="@item.OrderLineId" data-iscustom="@item.IsCustom">
                        <td>@item.OrderId</td>
                        <td>@item.OrderDate</td>
                        <td>@item.RequestedDeliveryDate</td>
                        <td>@item.ProductName</td>
                        <td>@item.Ident</td>
                        <td>@item.CompanyName</td>
                        <td>@item.DepartmentName</td>
                        <td>@item.ProdAlias</td>
                        <td>@item.ProducerName</td>
                        <td>@item.ProductionInfo</td>
                    </tr>
                }

Button which starts the JavaScript function:

 <button class="btn waves-effect waves-light btn-success" onclick="ProcessMultipleRows();">Start</button>

JavaScript function:

  function ProcessMultipleRows() {
            if ($(".dataTables_scrollBody>tr.selected").length > 0) {
                var list = [];
                $(".dataTables_scrollBody>tr.selected").each(function (e) {
                    var element = $(this);
                    var orderid = element.data("orderid");
                    var iscustom = element.data("iscustom");
                    var orderlineid = element.data("orderlineid");
                    var folderPath = "";
                    var fileName = "";

                    list.push({ orderId: orderid, isCustomOrderLine: iscustom, orderLineId: orderlineid, folderPath: folderPath, fileName : fileName});
                });

                $.ajax({
                    url: '@Url.Action("StartWorkflow","OrderLines")',
                    type: "post", //<------------- this is important
                    data: { model: list }, //<------------- this is important
                    beforeSend: function (xhr) {//<--- This is important
                      xhr.setRequestHeader("RequestVerificationToken",
                      $('input:hidden[name="__RequestVerificationToken"]').val());
                      showPreloader();
                    },
                    success: function (data) {

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

                    },
                     complete: function () {
                         hidePreloader();
                    }
                });
            }
        }

MVC action:

[HttpPost]
[ValidateAntiForgeryToken] //<--- This is important
public async Task<IActionResult> StartWorkflow(IEnumerable<WorkflowModel> model)

And MODEL in C#:

public class WorkflowModel
 {
        public int OrderId { get; set; }
        public int OrderLineId { get; set; }
        public bool IsCustomOrderLine { get; set; }
        public string FolderPath { get; set; }
        public string FileName { get; set; }
 }

CONCLUSION:

The reason for ERROR:

"Failed to load resource: the server responded with a status of 400 (Bad Request)"

Is attribute: [ValidateAntiForgeryToken] for the MVC action StartWorkflow

Solution in Ajax call:

  beforeSend: function (xhr) {//<--- This is important
                      xhr.setRequestHeader("RequestVerificationToken",
                      $('input:hidden[name="__RequestVerificationToken"]').val());
                    },

To send List of objects you need to form data like in example (populating list object) and:

data: { model: list },

type: "post",

ticky
  • 363
  • 2
  • 12
0

Nothing worked for me in asp.net core 3.1. Tried all the above approaches. If nothing is working and someone is reading the rows from table and wanted to pass it to Action method try below approach... This will definitely work...

<script type="text/javascript">
    $(document).ready(function () {

        var data = new Array();
      var things = {};

      // make sure id and color properties match with model (Thing) properties
      things.id = 1;
      things.color = 'green';

      data.push(things);

      Try the same thing for dynamic data as well that is coming from table.

        // var things = [ { id: 1, color: 'yellow' }, { id: 2, color: 'blue' }, { id: 3, color: 'red' } ];

        $.ajax({
            contentType: 'application/json;',
            type: 'POST',
            url: 'your url goes here',
            data: JSON.stringify(things)
        });
    });
</script>

public class ThingController : Controller
{
    public void PassThing([FromBody] List<Thing> things)
    {
        // do stuff with things here...
    }

    public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }
}
0

The way that worked for me

            $.ajax({
            url: '@Url.Action("saveVideoesCheckSheet", "checkSheets")',
            type: "POST",
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: "data=" + JSON.stringify(videos),
            success: function (response) {
                window.location.reload(true);
            },
            failure: function (msg) {
                
            }
        });
    });

Controller

       public  string saveVideoesCheckSheet(string data)
    {

        List<editvideo> lst = JsonConvert.DeserializeObject<List<editvideo>>(data);...}

After much experimentation, bit of a cheat.

aegsomweb
  • 83
  • 1
  • 9
0

Send data from the list checkbox clicked to an array and pass the list type of the model in the controller to create a new model as an array name match and declare the model property you want from the view page.

<script>
    function save() {
        alert('hi'); 
        var cartridges = new Array();
        $("#tbl1  input:checked ").each(function () {
            var row = $(this).parents("tr");
            var cartridge = {};
            cartridge.Id = row.find("TD").eq(0).html();
            cartridge.Name = row.find("TD").eq(1).html();
            cartridges.push( cartridge );
        });
        //alert(JSON.stringify(customers) + "rfgdfcd");
        $.ajax({
            type: 'post',
            url: '@Url.Action("saveDetails", "Home")',
            data: JSON.stringify(cartridges),
            dataType: "json",
            contentType: 'application/json',
            success: function (response) {
                alert("succese");
            },
                failure: function (response) {
                    alert("fail");
            },
                error: function (response) {
                    alert("error");
            },
        });
    }
</script>
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '23 at 00:49
  • how can we add file upload to it. – Heemanshu Bhalla Apr 17 '23 at 11:43