首页 > 解决方案 > 将 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 但这似乎永远行不通,是否有更好的方法来归档我要归档的内容?

标签: c#asp.netasp.net-web-api

解决方案


尝试使用此控制器操作标头:

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

推荐阅读