c# - 将 Int 数组传递给 ASP .NET 控制器
问题描述
我正在尝试将数组传递给控制器以更新那些匹配的记录这是我传递数组的方式:
[Route("api/updateFile/{id}")]
[HttpGet]
[HttpPost]
public void updateFile(int[] ids, string FolderName, Int Key)
{
var FilesToUpdate = db.ActivityFiles.FirstOrDefault(x => x.id == ids); //ids containing 124,52,22,262,32
FilesToUpdate.id= FolderName;
FilesToUpdate.Key = Key;
_db.SaveChanges();
}
Ajax 非常简单
$.ajax({url: '/api/updateFile/' + imgList + '?FolderName=' + 545454 + '&Key=' + 777,
type: "GET",success: function (data) {console.log("image set updated - success! ");},
error: function (data) {}});
imgList 包含:124,52,22,262,32
但这似乎永远行不通,是否有更好的方法来归档我要归档的内容?
解决方案
尝试使用此控制器操作标头:
[Route("api/updateFile/{folderName}/{key}")]
public void updateFile([FromBody]int[] ids, string folderName, int key)
{
var filesToUpdate = db.ActivityFiles.FirstOrDefault(x => ids.Contains(x.id) );
filesToUpdate.id= folderName;
filesToUpdate.Key = key;
_db.SaveChanges();
}
这个阿贾克斯:
$.ajax({
url: '/api/updateFile/545454/777',
data: [124,52,22,262,32],
success: function (result) {console.log("image set updated - success! ");},
error: function (err) {}
});
推荐阅读
- python - 如何从 Pandas 的索引中删除括号“()”?
- asp.net - ASP.NET下实际使用的是什么版本的.NET框架
- vba - 检查空白输入框并在 VBA 中发送适当消息的问题
- bixby - 如何跟踪 Bixby 中打开的详细信息视图?
- r - Conditionnaly 拆分数据帧 R 中的字符串单元格
- git - 如何实现压缩合并提交
- java - 使用后如何将 StringJoiner 重置为默认状态
- python - Python Selenium - 尝试按类名查找元素时出错
- javascript - 无法从其父 React JS 获取子组件引用对象
- vba - VBA日期格式方法给出不同的世纪