You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC中如何持久化练习配置参数生成题目?

问题:ASP.NET Core MVC 练习应用的参数持久化方案选择

输入图片描述

我是ASP.NET Core MVC及编程领域的新手,正在做一款模拟赌场荷官骰子赔付计算的练习应用:

  • 赌注:图中的30是WORLD BET的投注金额
  • 骰子点数:3,赔付计算规则会根据点数不同而变化
    用户需要提交正确答案(此场景下为66)。

我需要添加设置功能来配置练习参数:比如设置最小赌注5、最大赌注100,生成该范围内的随机题目;或设置最小赌注25、最大赌注500、增量25,生成25、50、75…500这类固定步长的赌注题目。

当前代码

WorldController 代码

public class WorldController : Controller
{
    private const EGames currentGame = EGames.WorldBet;

    private readonly UserManager<User> _userManager;
    private readonly ApplicationDbContext _context;

    public WorldController(UserManager<User> userManager, ApplicationDbContext context)
    {
        _userManager = userManager;
        _context = context;
    }

    public IActionResult Play(int? min, int? max, int? increment)
    {
        PropBet worldBet = GenerateWorlBet(min, max, increment);
        return View(worldBet);
    }

    [HttpPost]
    [Authorize]
    public async Task<IActionResult> Play(PropBet propBet)
    {
        User? user = await _userManager.GetUserAsync(HttpContext.User);

        PlayerGames? worldBetGame = await _context.PlayerGames.FirstOrDefaultAsync(g => g.UserId == user!.Id && g.GameType == currentGame);

        if (worldBetGame is null)
        {
            worldBetGame = new PlayerGames { GameType = currentGame, UserId = user!.Id, TempBestResult = 0, LongestCorrectAsnwerStreak = 0, TotalAnswers = 0 };

            await _context.PlayerGames.AddAsync(worldBetGame);
            await _context.SaveChangesAsync();
        }

        if (propBet.Answer == CalculateCorrectAnswer(propBet.Bet, propBet.RolledNumber))
        {
            worldBetGame.TempBestResult++;
            worldBetGame.TotalAnswers++;

            if (worldBetGame.TempBestResult > worldBetGame.LongestCorrectAsnwerStreak)
            {
                worldBetGame.LongestCorrectAsnwerStreak = worldBetGame.TempBestResult;
                await _context.SaveChangesAsync();
            }

            return RedirectToAction(nameof(Play));
        }
        else
        {
            propBet.ErrorMessage = "Wrong Payout!";
            worldBetGame.TempBestResult = 0;
            worldBetGame.TotalAnswers++;

            await _context.SaveChangesAsync();

            return View(propBet);
        }
    }
}

Play 视图代码

@model PropBet
@{
    ViewData["Title"] = "Play";
}

<div class="container">
    
    <hr />
    <div>

        <h2>World Bet</h2>

        @if (Model.ErrorMessage != null)
        {
            <p>@Model.ErrorMessage</p>
        }

        <p>Bet: @Model.Bet</p>
        <p>Dice Roll: @Model.RolledNumber</p>
        <div class="row">
            <form asp-action="Play">
                <input asp-for="@Model.Bet" name="bet" hidden />
                <input asp-for="@Model.RolledNumber" name="rolledNumber" hidden />

                <div class="form-group">
                    <label for="min">Min Bet</label>
                    <input type="number" id="min" name="min" min="5" max="5000" step="5" />
                </div>

                <div class="form-group">
                    <label for="min">Max Bet</label>
                    <input type="number" id="max" name="max" min="5" max="5000" step="5" />
                </div>

                <div class="form-group">
                    <label for="min">Increment</label>
                    <select id="increment" name="increment">
                        <option value="5" selected>5</option>
                        <option value="25">25</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select>
                </div>

                <div class="form-group">
                    <label asp-for="@Model.Answer">Answer:</label>
                    <input asp-for="@Model.Answer" type="number" min="0" required autofocus />
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Submit Answer</button>
                </div>
            </form>
        </div>
    </div>
</div>

遇到的问题

我需要将设置参数(min、max、increment)传递给GenerateWorlBet(min, max, increment);方法。最初尝试把设置放在表单里提交,但提交后会进入[HttpPost]的Play方法,若答案正确会重定向到[HttpGet]的Play方法,导致参数丢失。

尝试的方案

  1. 控制器成员变量存储:在控制器中添加Min、Max、Increment成员变量,在Post方法里赋值,修改Get方法用这些变量生成题目。但不确定是否可行,因为控制器实例是每次请求创建的,成员变量没法跨请求保存。
private const EGames currentGame = EGames.WorldBet;

private readonly UserManager<User> _userManager;
private readonly ApplicationDbContext _context;

int Min;
int Max;
int Increment;

[HttpPost]
[Authorize]
public async Task<IActionResult> Play(PropBet propBet, int? min, int? max, int? increment)
{
    // 赋值操作
    Min = min ?? 5;
    Max = max ?? 100;
    Increment = increment ?? 5;

    // 业务逻辑
    return RedirectToAction(nameof(Play));
}

public IActionResult Play()
{
    PropBet worldBet = GenerateWorlBet(Min, Max, Increment);
    return View(worldBet);
}
  1. ViewData/ViewBag:听说可以用这两个对象传递数据,但从没使用过,不确定是否适合这个场景。

希望得到最优方案或更简单的解决方案建议。


内容的提问来源于stack exchange,提问作者Console.WL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:44:55