0

I am passing a List of IDs to a controller method in my .NET MVC project with an Ajax call.

But sometimes there are no IDs to pass and so I want to either pass an empty list or null.

However instead of an empty list or null it passes a List with a single ID of 0.

Here is my controller method:

public ActionResult MultipleDaysModal(List<int> IDs)
{
    if (IDs.Any())
    {
        ViewBag.CurrentDays = ReservationHelper.GetBlockoutsByIDs(IDs)
                                               .OrderBy(i => i.BlockoutTime1)
                                               .ToList();
    }
    return View("ReservationPartials/_MultipleDaysModal");
}

Below is my first attempt at the Ajax method:

function loadMultipleDays() {
    var blockoutIDString = $('#blockoutIDs').val();
    // had to split, otherwise the controller didn't recognize it as a list of ints
    var blockoutIDs = blockoutIDString.split(',');
    $.ajax({
        url: '@Url.Action("MultipleDaysModal", "Reservations")',
        method: 'GET',
        traditional: true,
        data: { 'IDs': blockoutIDs },
        success: function (data) {
            // Do stuff
        },
        error: function (xhr) {
            // DO stuff
        }
    });
}

When I got the problem of the non-empty list I tried to force it to be null:

function loadMultipleDays() {
    var blockoutIDString = $('#blockoutIDs').val();
    // had to split, otherwise the controller didn't recognize it as a list of ints
    var blockoutIDs = blockoutIDString ? blockoutIDString.split(',') : null;
    // Ajax call . . .
}

However I still got the same List with a single ID of 0. This bypasses my check of IDs.Any(). I know I can change the if statement to check differently, however I would like to understand why this is happening.

Edit: When I step through the javascript in devtools the variable blockoutIDs is in fact set to null (since I currently have the second attempt running). When looking at the request the query string parameters shows 'IDs=' and as JSON 'IDs:' so nothing.

Mr. Spock
  • 315
  • 1
  • 9
  • 2
    In your browser dev tools, what is the browser *actually* sending in the JSON payload? –  Mar 10 '21 at 20:38
  • @Amy I just edited my question, I meant to originally include those details but forgot to – Mr. Spock Mar 10 '21 at 20:43
  • The why... [15.5.4.14 String.prototype.split (separator, limit)](http://es5.github.io/#x15.5.4.14) _If the `this` object is (or converts to) the empty String, the result depends on whether separator can match the empty String. "If it can, the result array contains no elements. Otherwise, the result array contains one element, which is the empty String."_ – quaabaam Mar 10 '21 at 21:12
  • @ Peter B Yes it does! I guess my search skills were failing me when I couldn't find it. Thanks to Amy too for the help! – Mr. Spock Mar 10 '21 at 21:12

1 Answers1

2

I think what you should do is the following. If you have no identifiers, don't send a payload at all.

var blockoutIDString = $('#blockoutIDs').val();
var data = blockoutIDString 
    ? { 'IDs': blockoutIDString.split(',') } 
    : null

$.ajax({
    url: '@Url.Action("MultipleDaysModal", "Reservations")',
    method: 'GET',
    traditional: true,
    data: data,
    success: function (data) {
        // Do stuff
    },
    error: function (xhr) {
        // DO stuff
    }
});