最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing a parameter of type List to Javascript function - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jul 16, 2014 at 8:26 Sahar Ch. asked May 29, 2014 at 9:00 Sahar Ch.Sahar Ch. 4991 gold badge9 silver badges28 bronze badges 9
  • List<> or JSON, if you print the list you say? it will be useful – vivek 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. – Sahar Ch. 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 – webketje 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 – Sahar Ch. 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 – user1741851 Commented May 29, 2014 at 10:38
 |  Show 4 more ments

1 Answer 1

Reset to default 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./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]);
};
发布评论

评论列表(0)

  1. 暂无评论