0

我正在做一个学校项目,但我被困住了。我正在尝试从用户那里获取一些信息,并使用用户输入的信息来计算一些东西。

我的代码正在运行,但我的老师告诉我我需要将计算替换到我的逻辑层(它们在我的控制器中)。我正在尝试这样做,但我被卡住了。

这是我的控制器:

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

        }

这是逻辑层中的类:

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;

            }
        }
    }

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

用户输入

如您所见,它被正确处理

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

4

2 回答 2

2

您正在将 a 传递给new CalculatorModel您对应用程序逻辑类的调用:

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
于 2020-10-27T12:46:59.707 回答
0

你的模型是 null ,首先你必须填写你的模型。像这样更改您的代码,

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

        }
于 2020-10-27T12:54:15.170 回答