6

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?

9
  • List<> or JSON, if you print the list you say? it will be useful Commented May 29, 2014 at 9:10
  • It's a list of integers (IDs) that I will pass to the function, the function is about deleting those instances by their IDs using an API. Commented May 29, 2014 at 9:13
  • With 'List', do you mean Live/ Static Nodelist or Array? This matters because the methods are different for both Commented May 29, 2014 at 9:45
  • Well, I'm confused now. I used a List for my API, and an Array for the js function.. Please see the edit, I think I messed up too much with my code Commented May 29, 2014 at 10:01
  • 1
    There's no List type in javaScript. Either there's Array or Object (I mean { }). However to send any of these as parameters, I suggest you use data: option available in jQuery $.ajax function Commented May 29, 2014 at 10:38

1 Answer 1

4

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]);
};
Sign up to request clarification or add additional context in comments.

2 Comments

Can I pass this Array parameter as a parameter to an API inside the function that I use in an ajax call? this API needs a parameter of type List (as its description in the controller says). That's what I need.
That's impossible to say without knowing what the API is, becuase we don't know how it's implementation works. It be easier if you show your code, and said what actual API you are calling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.