JDoeBloke
JDoeBloke

Reputation: 591

Pass Data by AJAX to Controller in MVC

I'm trying to pass data to controller for further processing but I get null in controller, however debug of js (ajax) shows the number anyway. What might be the problem?

Ajax:

 $('.toggler_btn').on('click', function (event)
            {
                var id = $(this).attr('data-id');
                    if ($(this).text() === '+') {
                        $.ajax({
                            url: "/List/GetSubItems",
                            type: "POST",
                            contentType: "html",
                            dataType: "text",
                            data: '{"id":"' + id + '"}', // here it show the data, like 5 or some other number
                            success: function (data)
                            {
                                $('.element_wrapper [data-id="' + id + '"]').html(data);
                            }
                            })
                        $(this).html('-');
                    }
                    else $(this).html('+');
            });

Controller:

  [HttpPost]
    public ActionResult GetSubItems(string id) // here I get null
    {
        int pID = 0;
        int.TryParse(id, out pID);
        List<H_Table> list = new List<H_Table>();

        foreach (var item in db_connection.H_Table.Where(i => i.PARENT_ID == pID))
        {
            list.Add(item);
        }
        return PartialView(list);
    }

Upvotes: 0

Views: 13761

Answers (4)

Zulqarnain Jalil
Zulqarnain Jalil

Reputation: 1691

$('.toggler_btn').on('click', function (event) {
    var id = $(this).attr('data-id');
    if ($(this).text() === '+') {
        $.ajax({
            url: '/List/GetSubItems',
            type: 'POST',
            dataType: 'json',
            data: '{"id":"' + id + '"}',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $('.element_wrapper [data-id="' + id + '"]').html(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("responseText=" + XMLHttpRequest.responseText + "\n textStatus=" + textStatus + "\n errorThrown=" + errorThrown);
            }
        });
        $(this).html('-');
    }
    else $(this).html('+');
});

Use this one just copy and paste

Upvotes: 2

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17506

Your AJAX request has set contentType: "html" but you are actually sending JSON (with data: '{"id":"' + id + '"}'). And your controller is receiving a string.

So either change your AJAX call to send a raw string:

contentType: "text",
data: { id: id }

...or update your controller to receive JSON. This latter can be achieved by creating something like this:

public ActionResult GetSubItems(ViewModel model)

public class ViewModel {
    public string Id { get; set; }
}

EDIT: also, for reference, you might want to see the difference between contentType and dataType.

Upvotes: 0

Saeed Ahmadian
Saeed Ahmadian

Reputation: 1119

Change data type to "application/json" and data to this :

   data :{
          id : id
   }

Upvotes: 0

Tharsan Sivakumar
Tharsan Sivakumar

Reputation: 6531

Change your content type to "text" and change the data as well.

Upvotes: 0

Related Questions