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 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]);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With