我讀過的所有其他帖子在這個主題上都沒有幫助我(告訴我[FromBody]在標題中放置或設定內容型別)。
我的js函式是:
async function GetEditModalHTML(productPriceGroupID) {
const data = {
ProductPriceGroupID: productPriceGroupID
}
const response = await fetch('/promotions/productprice/edit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
},
body: JSON.stringify(data)
});
return await response.text();
}
以及控制器中的函式定義:
[HttpPost]
public async Task<IActionResult> Edit([FromBody] int productPriceGroupID)
在 Rider 中除錯我知道該操作已正確呼叫。在 chrome 網路選項卡中,我可以看到有效負載為:
![即使設定了 [FromBody] 和 Content-Type,Controller Action 也不會使用 JS Fetch API 接受 JSON post](https://img.uj5u.com/2022/11/17/033a15e682af492e8402ee0b239790df.png)
但無論我嘗試做什么,值productPriceGroupID始終為 0。為什么?
uj5u.com熱心網友回復:
創建類傳遞資料
class ProductDetails{
public int productPriceGroupID {get;set;}
}
然后分配
[HttpPost]
public async Task<IActionResult> Edit([FromBody] ProductDetails productPriceGroupID)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/535863.html
