简体   繁体   中英

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);
    }
}

Car Class

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

CarModel

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

I can add "customers" and "model" parameters to my "Car" class. But I don't want to add "customers" and "model" parameters to my "Car" class

How can I solve this problem in other ways.

Error在此处输入图像描述

If you post ALL this input parameters from your request body, you have to create ViewModel:

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

But I don't think you need all this properties, you can merge some.

Change your action:

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

Replace the model in a view with:

@model CarViewModel

and fix your view controls data binding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM