我正在提取以更改我的記錄,并且該值newInsert在提取中為真,但在控制器中為假。
拿來:
fetch('api/Test/UpdateOrInsertType', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify({
'newInsert': newInsert //Console.log -> true
})
控制器:
[HttpPost("UpdateOrInsertType")]
public IActionResult UpdateOrInsertType([FromBody] bool newInsert)
// Debugger newInsert -> false
{
try
{
return Ok(Test.UpdateOrInsertType(newInsert));
}
catch (Exception ex)
{
return Conflict(ex);
}
}
uj5u.com熱心網友回復:
Json 不能與原始型別一起正常作業。如果你不使用 json.stringify 你必須創建一個 ViewModel
public class ViewModel
{
public bool NewInsert {get; set;}
}
和行動
[HttpPost("UpdateOrInsertType")]
public IActionResult UpdateOrInsertType([FromBody] ViewModel model)
{
bool newInsert=model.NewInsert
或者你可以洗掉 { 'Content-Type': 'application/json' }
fetch('api/Test/UpdateOrInsertType', {
method: 'POST',
body: { newInsert: newInsert }
})
并洗掉 [FromBody]
[HttpPost("UpdateOrInsertType")]
public IActionResult UpdateOrInsertType( bool newInsert)
uj5u.com熱心網友回復:
在這種情況下,您不應使用JSON.stringify.
只需將原始 javascript 物件傳遞給fetch body:
fetch('api/Test/UpdateOrInsertType', {
method: 'POST',
body:
{
newInsert: newInsert
}
});
現在在服務器端,它將被識別為一個bool屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395545.html
標籤:javascript C# asp.net-mvc 控制器
