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

ASP.NET Core MVC中Model.Algorithm无法保持选中状态,提交后切回Caesar

解决加密网页提交后算法选中状态重置的问题

问题核心是下拉框未根据Model中保存的算法值设置选中状态,导致每次提交页面刷新后默认选中第一个选项。按以下步骤修复:

1. 确保Model包含Algorithm属性

在你的Model类中添加Algorithm字段,用于存储用户选中的算法:

public class EncryptionModel
{
    public string PlainText { get; set; }
    public string Algorithm { get; set; } // 保存选中的算法
    public string Cipher { get; set; }
}

2. Controller中保留算法状态

在Post请求处理方法中,接收并保留Algorithm的值,处理完加密后将完整的Model返回给视图:

[HttpPost]
public IActionResult Index(EncryptionModel model)
{
    // 根据选中的算法执行加密逻辑
    switch(model.Algorithm)
    {
        case "caesar":
            model.Cipher = CaesarEncrypt(model.PlainText); // 替换为你的凯撒加密实现
            break;
        case "vigenere":
            model.Cipher = VigenereEncrypt(model.PlainText); // 替换为你的维吉尼亚加密实现
            break;
        case "railfence":
            model.Cipher = RailFenceEncrypt(model.PlainText); // 替换为你的栅栏加密实现
            break;
        case "sha256":
            model.Cipher = Sha256Encrypt(model.PlainText); // 替换为你的SHA256实现
            break;
    }
    return View(model); // 返回包含算法状态的Model
}

3. 修改视图中的下拉框绑定

有两种方式实现选中状态的自动绑定:

方式一:手动添加selected判断

在每个option标签中添加条件判断,当选项值与Model.Algorithm匹配时设置selected属性:

<form method="post">
    <div class="form-group">
        <label for="plainText">Plain Text:</label>
        <input type="text" class="form-control" id="plainText" name="plainText" value="@Model.PlainText">
    </div>
    <div class="form-group">
        <label for="algorithm">Algorithm:</label>
        <select class="form-control" id="algorithm" name="algorithm">
            <option value="caesar" @(Model.Algorithm == "caesar" ? "selected" : "")>Caesar Cipher</option>
            <option value="vigenere" @(Model.Algorithm == "vigenere" ? "selected" : "")>Vigenere Cipher</option>
            <option value="railfence" @(Model.Algorithm == "railfence" ? "selected" : "")>Rail Fence Cipher</option>
            <option value="sha256" @(Model.Algorithm == "sha256" ? "selected" : "")>sha256 Cipher</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Encrypt</button>
</form>

@if (!string.IsNullOrEmpty(Model.Cipher))
{
    <hr />
    <h2>Encrypted Text:</h2>
    <p>@Model.Cipher</p>
}

方式二:使用ASP.NET Core Tag Helper(更简洁)

用asp-for标签助手自动绑定Model的Algorithm属性,框架会自动处理选中状态:

<form method="post">
    <div class="form-group">
        <label for="plainText">Plain Text:</label>
        <input type="text" class="form-control" id="plainText" asp-for="PlainText">
    </div>
    <div class="form-group">
        <label asp-for="Algorithm">Algorithm:</label>
        <select class="form-control" asp-for="Algorithm">
            <option value="caesar">Caesar Cipher</option>
            <option value="vigenere">Vigenere Cipher</option>
            <option value="railfence">Rail Fence Cipher</option>
            <option value="sha256">sha256 Cipher</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Encrypt</button>
</form>

@if (!string.IsNullOrEmpty(Model.Cipher))
{
    <hr />
    <h2>Encrypted Text:</h2>
    <p>@Model.Cipher</p>
}

这样处理后,提交表单时选中的算法会被保存到Model中,页面刷新后会自动保持选中状态,直到用户切换其他算法。

内容的提问来源于stack exchange,提问作者DennisM

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 08:23:14