传递一个List类型的参数给Javascript函数
Passing a parameter of type List to Javascript function
我的视图中有一个函数,它需要调用一个列表,以便在其中执行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();
}
请问这个问题有解决方案或变通方法吗?
我假设在您的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]);
};
相关文章:
- 从查询字符串参数推断出正确的数据类型
- 为什么不'我们在javascript中使用函数参数的数据类型
- 如何确保函数/方法参数为certian类型
- 模版启动错误-类型错误(“参数”url'必须是字符串,而不是“+类型的url”)
- JS:执行'失败;getComputedStyle'在'窗口':参数的类型不是'
- 如何在谷歌电子表格脚本中测试范围参数的类型
- Angular 2:如何在 GET 和 POST 请求中设置 double、float、int 和布尔类型参数
- 收到错误:无法在“节点”上执行“appendChild”:参数 1 的类型不是“节点”
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- PhpStorm警告:参数类型不可分配给参数类型
- 当数据类型为XML时,如何将url参数传递给AJAX jquery
- TypeScript-如何定义具有任何类型参数的函数类型
- AngularJS $http.post ASP.NET 具有简单类型参数的 Web API 控制器
- JavaScript 类型参数的有效性
- 获取引导预类型参数
- 错误TS2314:泛型类型'Promise<需要1个类型参数
- RegisterClientScriptInclude:什么是“类型”?参数
- Dojo 1.7 Dojo /on可以接受哪些字符串作为类型参数
- JavaScript's文档.打开支持MIME类型参数
- 流类型:泛型Id<T>具有与传入的类型参数类似约束的类型