POST后缺少密码值

本文关键字:密码 POST | 更新日期: 2023-09-27 18:15:37

下面是一些代码:

控制器

[HttpGet]
public ActionResult GetSettings()
{
    var save = new SettingsSaver();
    var dto = save.GetSettings();
    var model = new SettingsModel
    {
        Password = dto.Password,
        Port = dto.Port,
        Username = dto.Username,
        Enabled = dto.Enabled,
        Id = dto.Id,
        IpAddress = dto.IpAddress,
    };
    return View(model);
}
[HttpPost]
public ActionResult GetSettings(SettingsModel viewModel)
{
    if (ModelState.IsValid)
    {
        var dto = new SettingsDto
        {
            IpAddress = viewModel.IpAddress,
            Password = viewModel.Password,
            Port = viewModel.Port,
            Username = viewModel.Username,
            Enabled = viewModel.Enabled,
            Id = viewModel.Id
        };
        var save = new SettingsSaver();
        var result = save.SaveSettings(dto); //Saves correctly and updates in DB
        if (result)
        {
            return View(); // Returns this
        }
        return View("Error");
    }
    return View("Error");
}

视图(默认编辑视图)

@model Dash.UI.Models.Settings.SettingsModel
@{
    ViewBag.Title = "Settings";
}
<h2>Settings</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Settings</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)
        <div class="form-group">
            @Html.LabelFor(model => model.Enabled, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Enabled)
                    @Html.ValidationMessageFor(model => model.Enabled, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.IpAddress, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.IpAddress, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.IpAddress, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Port, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Port, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Port, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

所以,问题是当我将模型和POST更新到GetSettings时,它都能正常工作,在数据库中更新等,但在return View()上,它没有击中GetSettings()动作方法,但它返回的视图中填充了除密码以外的所有模型。

public class SettingsModel : BaseSettingsViewModel // Base contains ID and Enabled properties with no data annotations
{
    [Required]
    [DataType(DataType.Text)]
    [DisplayName("IP Address")]
    public string IpAddress { get; set; }
    [Required]
    [DisplayName("Port")]
    public int Port { get; set; }
    [Required]
    [DataType(DataType.Text)]
    [DisplayName("Username")]
    public string Username { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }
}

任何建议/指导将不胜感激。

POST后缺少密码值

当从POST返回相同的视图时,ModelState将根据提交的值填充控件(密码字段除外)。

所以你需要Post-Redirect-Get模式:
if (result)
{
    return RedirectToAction("GetSettings");
}