将List类型的参数传递给Javascript函数

时间:2014-05-29 09:00:26

标签: javascript list

我的视图中有一个函数,需要调用一个列表,以便在其中执行ajax调用。我正在使用的API在其参数中有一个参数List<>。这就是我需要它的原因。

是否可以将List<>类型的参数传递给Javascript函数?如果是,使用什么是合适的语法?我用谷歌搜索,但还没有找到答案。

编辑:这是我的代码

Javascript功能:

    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
            }
        });
    }

调用使用此功能的弹出窗口:

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

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();
    }

请问这个问题有解决方案或解决方法吗?

1 个答案:

答案 0 :(得分:0)

我认为在您的javascript中,List<>Array

如果是这种情况,你可以用它做各种事情:http://www.w3schools.com/js/js_arrays.asp

示例(调用此函数将显示数组中每个元素的警报):

var alertAllElements = function (theArray) {
    for(var x=0; x<theArray.length; x++)
        alert(theArray[x]);
};