This is a migrated thread and some comments may be shown as answers.

The data passed through an AJAX call to the controller is null

1 Answer 3117 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jack
Top achievements
Rank 1
Jack asked on 18 Apr 2018, 01:25 PM

Good day,

 

I have a question since all solutions I've found so far didn't work in my case. Hre's a brief description of what I have... So, I have my kendoGrid with all the info displayed. I'm trying to make a button for certain rows which will call a method in my Controller. Here's the code on my page:

@(Html.Kendo().Grid<DisplayEntitiesViewModel>()
              .Name("CustomersGrid")
              .Columns(columns =>
              {
                  columns.Bound(c => c.CustomerId).Hidden(true);
                  columns.Bound(c => c.CustomerName)
                      .Width(150);
                  columns.Bound(c => c.CustEmail)
                      .Width(800);
                  columns.Command(command => command
                      .Custom("Add Contact")
                      .Visible("showButton")
                      .Click("AddContact")).Width(180);
              })
              .HtmlAttributes(new {style = "height: 580px; width: 1133px; margin-left: 16px"})
              .Scrollable()
              .Groupable()
              .Sortable()
              .Pageable(pageable => pageable
                  .Refresh(true)
                  .PageSizes(true)
                  .ButtonCount(5))
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .Read(read => read.Action("ReadCustomers", "MyController"))
                  .PageSize(20)
              )
              )

 

Here's my script with the AJAX call:

function AddContact(c) {
 
        var row = $(c.target).closest("tr");
        var grid = $('#acombaGrid').data('kendoGrid');
        var dataItem = grid.dataItem(row);
 
        $.ajax({
            url: '@Url.Action("DispalyEntities", "MyController")',
            dataType: "json",
            contentType: "application/json",
            data: {
                customerId: dataItem.CustomerId
            },
            cache: false,
            type: "POST"
        });
    }

 

Here's in brief my Controller:

public IActionResult DispalyEntities(string customerId)
{
    return View();
}

 

My problem is, no matter what I've done so far... my costomerId passed to the controller is always null. Could you please point me what's wrong with it. Thanks in advance for doing so.

1 Answer, 1 is accepted

Sort by
0
Jack
Top achievements
Rank 1
answered on 18 Apr 2018, 03:56 PM
Nevermind... Found my stupid mistake. Should use GET instead. Thanks anyway.
Tags
Grid
Asked by
Jack
Top achievements
Rank 1
Answers by
Jack
Top achievements
Rank 1
Share this question
or