ASP.NET, C# |如何在控制器中使用方法(和返回值)?

ASP.NET, C# | How to use a method (and the returned value) with a 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);

        }

这是逻辑层中的class:

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;

            }
        }
    }

当我尝试使用断点查看哪里出错时,我可以看到用户的输入被正确定向到 class,但我的计算没有完成,我总是得到0 return 回来了。我不知道现在该怎么办(通常我用断点修复它,但它们对我没有帮助)

The user input

It is handled correctly as you can see

但我在控制器中的 Y 始终保持为零。 如果我在第一个 If 语句中使用 return 的方式,或者我在第二个或第三个 if 语句中使用 return 的方式,都没有区别。 有人可以帮助我吗?

您正在将 new CalculatorModel 传递到您对应用程序逻辑的调用中 class:

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

改成这样:

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

您的模型为空,首先您必须填写您的模型。像这样更改您的代码,

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

        }

 [HttpPost]
        public ActionResult PackCalculator(CalculatorModel calculator)
        {
            CalculatorLogic x = new CalculatorLogic();
            var model = new CalculatorModel(){
            CurrentLevel = 12,
            CurrentLevelPack = 10 .....
            };
            int Y = x.calculator(model);
            return View(calculator);

        }