1

I am injecting two classes in my controller.

In the get request, when I put a break point, I can see there is an instance being retrieved in the constructor .

However in the post action, the instance is null ?

here is my controller class;

using BethanyPieShop.Models;
using Microsoft.AspNetCore.Mvc;

namespace BethanyPieShop.Controllers
{
    public class OrderController : Controller
    {
        public IOrderRepository _orderRepository { get; set; }
        public ShoppingCart _shoppingCart { get; set; }

        public OrderController(IOrderRepository _orderRepository, ShoppingCart _shoppingCart)
        {
            _orderRepository = _orderRepository;
            _shoppingCart = _shoppingCart;
        }
        public IActionResult Checkout()
        {

            return View();
        }

        [HttpPost]
        public IActionResult Checkout(Order order)
        {
            // Null here _shoppingCart
            var items = _shoppingCart.GetShoppingCartItems();
            _shoppingCart.ShoppingCartItems = items;

            if (_shoppingCart.ShoppingCartItems.Count == 0)
            {
                ModelState.AddModelError("", "Your cart is empty, add some pies first");
            }

            if (ModelState.IsValid)
            {
                _orderRepository.CreateOrder(order);
                _shoppingCart.ClearCart();
                return RedirectToAction("CheckoutComplete");
            }
            return View(order);
        }


        public IActionResult CheckoutComplete()
        {
            ViewBag.CheckoutCompleteMessage = ", thanks for your order. You'll soon enjoy our delicious pies!";
            return View();
        }


    }
}

I registered the services in the main class:

builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<ShoppingCart>();
2
  • 2
    Your parameters have the same name as the fields so the hide them. I suspect you already get compiler warnings about this. Your code is assigning the parameter value to the parameter itself Commented Feb 23, 2022 at 13:29
  • 2
    Don't use leading underscores in parameter names. By convention a leading underscore is only used in private fields. Commented Feb 23, 2022 at 13:32

2 Answers 2

5

You are setting a variable with the same name. Without using a 'this.' statement. Try changing the following from

public OrderController(IOrderRepository _orderRepository, ShoppingCart _shoppingCart)
{
    _orderRepository = _orderRepository;
    _shoppingCart = _shoppingCart;
}

to

public OrderController(IOrderRepository orderRepository, ShoppingCart shoppingCart)
    {
        _orderRepository = orderRepository;
        _shoppingCart = shoppingCart;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

It's worth explaining why the problem occurred in the first place - the parameters are defined in the constructor's scope so they hide the fields with the same name defined in an outer scope. I think the compiler already generates a warning for this. Resharper certainly does
1

So I think you should use this. Like this:

public OrderController(IOrderRepository _orderRepository, ShoppingCart _shoppingCart)
    {
        this._orderRepository = _orderRepository;
        this._shoppingCart = _shoppingCart;
    }

4 Comments

The real solution would be to not use an underscore in a public member parameter
True, I personly would make those variables private readonly.
@PanagiotisKanavos Thanks i wll change it
@JorneSchuurmans thanks I wll change it

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.