Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a parameter of type List to Javascript function

I have a function in my view, that requires calling a list, in order to execute the ajax call within it. The API that I'm using has a parameter List<> among its arguments. That's why I need it.

Is it possible to pass a parameter of type List<> to a Javascript function? If yes, what's the appropriate syntax to use? I googled and didn't find an answer yet.

EDIT : Here's my code

Javascript function:

    function DeleteRoom(RoomId, FloorId, userDevicesID) {
        $.ajax({
            beforeSend: function (xhr) {
                xhr.setRequestHeader('verifySession', verifySession());
                xhr.setRequestHeader('Authorization', '@HttpContext.Current.Session["BaseAuth"]');
            },
            url: "/api/Room/RemoveRoom?RoomId=" + RoomId + "&userDevicesId="+ userDevicesID,
            type: "DELETE",
            dataType: 'json',
            success: function (data) {
                // removing items from the view
            }
        });
    }

Calling the popup that uses this function:

     arr.push(existingdevices[i].attrs.id); //array of IDs, can be empty
     PopUpDeleteRoom(id, FloorId, arr);

The API:

    [HttpDelete]
    public void RemoveRoom(int roomId, [FromUri]List<int> userDevicesId)
    {
        int currentUserId = SessionData.CurrentUser.UserID;
        List<Equipment> equipmentsAssociated = equipmentRepository.FindMany(e => e.RoomID == roomId).ToList();
        foreach (Equipment equipment in equipmentsAssociated)
        {
            equipment.RoomID = null;
            equipmentRepository.Update(equipment);
            equipmentDeviceRepository.DeleteAllEquipmentDeviceOfZwave(equipment.EquipmentID);
        }
        foreach (int userDeviceId in userDevicesId)
        {
            userDeviceRepository.Delete(userDeviceId); 
            //this generates a NullReferenceException that I will fix later
        }
        equipmentRepository.Save();
        userDeviceRepository.Save();
        roomRepository.Delete(roomId);
        roomRepository.Save();
    }

Please is there a solution or a workaround for this issue?

like image 912
Sahar Ch. Avatar asked Nov 10 '22 07:11

Sahar Ch.


1 Answers

I assume that in your javascript the List<> is an Array.

If that is the case, you can do all sorts of things with it: http://www.w3schools.com/js/js_arrays.asp

Example (calling this function will display an alert for each element in the array):

var alertAllElements = function (theArray) {
    for(var x=0; x<theArray.length; x++)
        alert(theArray[x]);
};
like image 129
Caleb Avatar answered Nov 14 '22 23:11

Caleb