我在我的视图中有一个函数,它需要调用一个列表,以便在其中执行ajax调用.我正在使用的API List<>在其参数中有一个参数.这就是我需要它的原因.
是否可以将类型的参数传递List<>给Javascript函数?如果是,使用什么是合适的语法?我用Google搜索,但还没有找到答案.
编辑:这是我的代码
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
}
});
}
Run Code Online (Sandbox Code Playgroud)
调用使用此函数的弹出窗口:
arr.push(existingdevices[i].attrs.id); //array of IDs, can be empty
PopUpDeleteRoom(id, FloorId, arr);
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
请问这个问题有解决方案或解决方法吗?
我假设在你的 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]);
};
Run Code Online (Sandbox Code Playgroud)