iT邦幫忙

0

ASP.NET Core Web API 更改多筆資料

我透過 Postman軟體測試更新多筆資料,偵錯時發現傳進來的值都是空的,請問有什麼方式可以一次更改多筆資料,謝謝各位大神。

[HttpPut("Signal_Updata")]
public async Task< IActionResult > Signal_Updata(List< thieanyear > thieanyears)
{
try
{
// 逐筆把資料庫資料修改
foreach (var itme in thieanyears)
{
var Light = _PDB.thieanyear.Where(a => a.oid == itme.oid).FirstOrDefault();
if (Light != null)
{
Light.year = thieanyears.year;
Light.well_name = thieanyears.well_name;
Light.eater_level = itme.eater_level;
}
await _PDB.SaveChangesAsync();
}

    ViewData.Add("ResultMessage", "資料編輯成功");
    return Json(new { data = "成功" });
}
catch (DbUpdateConcurrencyException)
{
    throw;
}

}

看更多先前的討論...收起先前的討論...
偵錯時發現傳進來的值都是空的是啥意思,是說List thieanyears 是空?
你API帶回來的資料是List thieanyears,還是?
看起來你更新並沒啥問題,如果List thieanyears有值的話
找到問題了 傳進來的參數 要設定 [FromBody] 不然他預設是 fromquery抓 會讀不到資料
public async Task< IActionResult > Signal_Updata([FromBody] List< thieanyear > thieanyears)
嗯 找到就好 光這個Method 沒前端看不出什麼端倪XD 線索太少
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
asd3733653
iT邦新手 2 級 ‧ 2021-10-08 11:02:01
最佳解答

thieanyears[i].year 你的i在哪裏?

看更多先前的回應...收起先前的回應...

另外我不建議這樣改,要是兩三千筆,妳就是在攻擊妳的資料庫

找到問題了 傳進來的參數 要設定 [FromBody] 不然他預設是 fromquery抓 會讀不到資料

意思是在網站中一次更改大量資料,會跑很久,設計上不太好對嗎?
請問有什麼比較好的方式更改多筆資料

Zack.EFCore.Batch
EF CORE的套件,詳細可以看他的用法

對 是非常不好 不是不太好

我要發表回答

立即登入回答