Search code examples
c#htmlbusiness-logic-layerpresentation-layer

ASP.NET, C# | How to use a method (and the returned value) with a controller?


I am working on a school project and I am stuck. I am trying to get some info from the user and calculate a few things with the inputted information from the user.

I had my code running, but my teacher told me I needed to replace the calculations to my logic layer (they were in my controller). I am trying to do that, but I am stuck.

This is my controller:

 public class CalculatorController : Controller
    {
        // GET: Calculator
        public ActionResult PackCalculator()
        {
            return View(new CalculatorModel());
        }

        [HttpPost]
        public ActionResult PackCalculator(CalculatorModel calculator)
        {
            CalculatorLogic x = new CalculatorLogic();
            int Y = x.calculator(new CalculatorModel());
            return View(calculator);

        }

And this is the class in the logic layer:

namespace ApexLogic
{
   public class CalculatorLogic
    {
        public int calculator(CalculatorModel calculator)
        {
            if (calculator.CurrentLevel < 21)
            {
                calculator.CurrentPack = calculator.CurrentPack + (calculator.CurrentLevel - 1);
                int seizoenPacks = calculator.PlayedSeasons * 5;
                int BattlepassPacks = calculator.Battlepass * 12;
                int CurrentPack = calculator.CurrentPack + seizoenPacks + BattlepassPacks + calculator.BoughtPacks;
                return CurrentPack;
            }

            else if (calculator.CurrentLevel > 21 && calculator.CurrentLevel < 301)
            {
                int CurrentLevelPack = calculator.CurrentLevel - 20;
                int PackCalculator2 = (calculator.CurrentLevel / 2);
                int CurrentLevelPack2 = PackCalculator2 + 19;
                int seizoenPacks = calculator.PlayedSeasons * 5;
                int BattlepassPacks = calculator.Battlepass * 12;
                calculator.CurrentPack = calculator.CurrentPack + seizoenPacks + BattlepassPacks +
                                         calculator.BoughtPacks + CurrentLevelPack2;
                return calculator.CurrentPack;


            }

            else if (calculator.CurrentLevel > 300 && calculator.CurrentLevel <= 500)
            {
                int CurrentLevelPack = calculator.CurrentLevel - 300;
                int PackCalculator2 = (CurrentLevelPack / 5);
                int CurrentLevelPack2 = PackCalculator2 + 159;
                int seizoenPacks = calculator.PlayedSeasons * 5;
                int BattlepassPacks = calculator.Battlepass * 12;
                calculator.CurrentPack = calculator.CurrentPack + seizoenPacks + BattlepassPacks +
                                         calculator.BoughtPacks + CurrentLevelPack2;
                return calculator.CurrentPack;
            }
            else
            {
                return 0;

            }
        }
    }

When I try and use the breakpoint to see where it goes wrong I can see that the input from the user is being directed correctly to the class, but my calculations are not being done and I always get 0 returned back. I have no idea what to do now (usually I get it fixed with breakpoints, but they are not helping me)

The user input

It is handled correctly as you can see

But my Y in the controller always stays zero. It makes no difference if I use the way of return like in the first If statements or if I use the way of return in the second or third if statement. Can someone help me?


Solution

  • You are passing a new CalculatorModel into your call to your application logic class:

    int Y = x.calculator(new CalculatorModel()); //This is what is messing you up
    

    Change it to this:

    int Y = x.calculator(calculator); //Use the model parameter which is passed into your Post method