提问者:小点点

“/”应用程序中的HTML,C#服务器错误(找不到资源)


我正在做一个小的学校项目,当我按我的计算按钮时,我得到了一个错误,我不知道为什么,或者如何修复它。我正在尝试为一个游戏(顶点传奇)做某种计算器。我一直得到错误:服务器错误在'/'应用程序中找不到资源。

以下是我的看法:

@model ApexLegendsApp.Models.CalculatorModel
@{
    ViewBag.Title = "PackCalculator";
}
<html>
<head>
    </head>
<body>
    <h2>PackCalculator</h2>
        <div class="row">
        @using (Html.BeginForm("/PackCalculator", "CalculatorModel", FormMethod.Post))
        {
            <div class="form-group">
                <label for="CurrentLevel">Current Level</label>
                <input type="text" class="form-control" id="CurrentLevel" name="CurrentLevel" value="@Model.CurrentLevel" />
            </div>

            <div class="form-group">
                <label for="Battlepass">Battle Pass Level</label>
                <input type="text" class="form-control" id="Battlepass" name="Battlepass" value="@Model.Battlepass" />
            </div>

            <div class="form-group">
                <label for="BoughtPacks">Bought Pack</label>
                <input type="text" class="form-control" id="BoughtPacks" name="BoughtPacks" value="@Model.BoughtPacks" />
            </div>

            <div class="form-group">
                <label for="PlayedSeasons">PlayedSeasons</label>
                <input type="text" class="form-control" id="PlayedSeasons" name="PlayedSeasons" value="@Model.PlayedSeasons" />
            </div>

            <div class="form-group">
                <label for="CurrentPack">Current Pack</label>
                <input type="text" class="form-control" id="CurrentPack" name="CurrentPack" value="@Model.CurrentPack" />
            </div>


            <button type="submit" class="btn btn-info" id="add" value="add" name="calculate">Calculate!</button>
        }

    </div>
    </body>
</html>

下面是我的模型:

namespace ApexLegendsApp.Models
{
    public class CalculatorModel
    {
        public int CurrentLevel { get; set; }
        public int Battlepass { get; set; }
        public int BoughtPacks { get; set; }
        public int PlayedSeasons { get; set; }
        public int CurrentPack { get; set; }
    }
}

这是我的控制器:

namespace ApexLegendsApp.Controllers
{
    public class CalculatorController : Controller
    {
        // GET: Calculator
        public ActionResult PackCalculator()
        {
            return View(new CalculatorModel());
        }
        [HttpPost]
        public ActionResult PackCalculator(CalculatorModel calculator, string calculate)
        {
            if (calculate == "add")
            {
                if (calculator.CurrentLevel < 21)
                {
                    calculator.CurrentPack = calculator.CurrentPack + (calculator.CurrentLevel - 1);
                    int seizoenPacks = calculator.PlayedSeasons * 5;
                    int BattlepassPacks = calculator.Battlepass * 12;
                    calculator.CurrentPack = calculator.CurrentPack + seizoenPacks + BattlepassPacks + calculator.BoughtPacks;
                    return View(calculator);
                }

                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 View(calculator);


                }

                else if (calculator.CurrentLevel > 300 && calculator.CurrentLevel <= 500)
                {
                    int CurrentLevelPack = calculator.CurrentLevel - 300;
                    int PackCalculator2 = (calculator.CurrentLevel / 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 View(calculator);

                }
            }
            else
            {
                return View(calculator);
            }
            return View(calculator);
        }
    }
}

我曾尝试改变路径,但我认为那不是问题所在。就像我说的,当我按calculate按钮时,它会返回错误。如果您需要另一个文件,只需注释它,我将添加该文件。

提前感谢你试图帮助我!!


共1个答案

匿名用户

问题出在form语句上。不要将/作为要发布到的操作方法(PackCalculator)的一部分,其次,必须将表单发布到CalculatorController,而不是CalculatorModel。控制器名称的controller部分是推断出来的,所以只需放入calculator作为控制器名称即可。

所以请更改以下内容:

@using (Html.BeginForm("/PackCalculator", "CalculatorModel", FormMethod.Post))

对此:

@using (Html.BeginForm("PackCalculator", "Calculator", FormMethod.Post))