繁体   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