簡體   English   中英

核心 Web Api - 錯誤:操作從請求正文綁定了多個參數

[英]Core Web Api - Error: Action has more than one parameter bound from request body

[ApiController]
[Route("test")]
public class AdminController : ControllerBase
{
    [HttpPost]
    public IActionResult Create(CarModel car, int[] customers, int model)
    {
        var item = new Car()
        {
            Name = car.Name,
            Price = car.Price
        };
        repository.Create(item, customers, model)
        return Ok(item);
    }
}

汽車 Class

public int Id { get; set; }
public string Name{ get; set; }
public int Price{ get; set; }

汽車模型

public int Id { get; set; }
public string Name{ get; set; }
public int Price{ get; set; }}

我可以將“客戶”和“型號”參數添加到我的“汽車”class。 但我不想將“客戶”和“型號”參數添加到我的“汽車”class

我怎樣才能以其他方式解決這個問題。

錯誤在此處輸入圖像描述

如果您從請求正文中發布所有這些輸入參數,則必須創建 ViewModel:

 public class CarViewModel
{
public CarModel car {get; set;} 
public int[] customers {get; set;}
public int model {get; set;}
}

但我認為你不需要所有這些屬性,你可以合並一些。

改變你的行動:

public IActionResult Create(CarViewModel model)
//or you can try, I don't know how you call your action
public IActionResult Create([FromBody] CarViewModel model)

將視圖中的 model 替換為:

@model CarViewModel

並修復您的視圖控件數據綁定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM