POST后密码值丢失

时间:2015-10-02 14:56:19

标签: c# asp.net-mvc

所以这里有一些代码:

控制器

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

非常感谢任何建议/指导。

1 个答案:

答案 0 :(得分:2)

从POST返回相同的视图后,ModelState将从发布的值中填写控件(除密码字段外)。

所以你需要一个Post-Redirect-Get模式:

if (result)
{
    return RedirectToAction("GetSettings");
}