2
[HttpPost("addtocart")]
        public IActionResult AddToCart( Cart cart,
                                        Product product,
                                        ProductOption productOption
                                       )
        {
            _cartService.Add(cart);
            _productService.Add(product);
            _productOptionService.Add(productOption);

            return NoContent();
        }

Why I cannot use IActionResult with multiple parameters and how to solve this issue

5
  • 1
    Presumably with a containing object that holds all 3. Is this ASP.NET or ASP.NET Core? Please only tag the one you're using. Commented Jun 3, 2021 at 9:40
  • @Llama Its a controller Commented Jun 3, 2021 at 9:40
  • Why does AddToCart seemingly serve to create a cart, product, and product option? I would expect a cart to exist, a product to exist, and for you to be adding an entry for that product in the cart, along with any chosen options. Commented Jun 3, 2021 at 9:42
  • It doesnt even work It gives error as soon as I click on start.. It says you cannot use multiple parameters. Commented Jun 3, 2021 at 9:43
  • Create a class that contains three properties (Cart, Product, and ProductOption). Make the method accept that. I still think your method functionality is probably very wrong though. Commented Jun 3, 2021 at 9:45

1 Answer 1

3

If you want to post all of those in 1 operation, you need to wrap them in some object.

public class Something
{
    public Cart Cart {get;set;}
    public Product Product {get;set;}
    public ProductOption ProductOption {get;set;}
}

and then take that in from the body of your request

[HttpPost("addtocart")]
public IActionResult AddToCart([FromBody] Something something)
{
    _cartService.Add(something.Cart);
    _productService.Add(something.Product);
    _productOptionService.Add(something.ProductOption);

    return NoContent();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.