MVC Post导致QueryString在重新加载相同视图时丢失

 观海望天 发布于 2023-01-02 14:08

请让我解释一下设置.

我有一个更改密码控制器/操作和视图.以下是我的帐户控制器中的操作签名:

public ActionResult ChangePassword(ChangePasswordMessageId? message)

[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)

首次加载更改密码时,我在查询字符串中有一些数据.这是一个例子:

https://www.mywebsite.com/Account/ChangePassword?mobile=1

这是视图中的Form声明.

@using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

表单通过简单的提交按钮提交:

表单有3个字段:当前密码,新密码和确认密码.如果用户正确填写所有数据,并通过所有客户端验证,表单工作正常.除了一个用例外,一切正常.

假设用户输入了不正确的旧密码值.当我进入上面的HTTPPOST ChangePassword操作时,它将失败.这就是代码的样子.

[HttpPost]
            public ActionResult ChangePassword(ChangePasswordViewModel model)
            {   
                if (ModelState.IsValid)
                {
                    try
                    {
                        MembershipUser user = Membership.GetUser();
        //The NEXT line is the one that fails if they supply the wrong Old Password value.
        //The code then falls to the catch condition below.
                        bool changePassword = user.ChangePassword(model.OldPassword, model.NewPassword);
                        if (changePassword)
                        {
                            string path = Url.Action("ChangePassword", new { Message = ChangePasswordMessageId.ChangePasswordSuccess });
                            temp = Request.UrlReferrer.ToString();
                            pos = temp.IndexOf("?");
                            if (pos > 0) path += "&" + temp.Substring(pos + 1);
                            return RedirectToLocal(path);    
                       }
                        else
                        {
                            ModelState.AddModelError("", "Change Password failed.");
                        }
                    }
                    catch //(Exception ex)
                    {
                        ModelState.AddModelError("", "Change Password failed.");
                        //ModelState.AddModelError("", ex.Message);
                    }
                }

                // If we got this far, something failed, redisplay form
    //The original query string will be gone. The URLwill now only show
    //https://www.mywebsite.com/Account/ChangePassword
                return View(model);
            }

是否可以称之为"返回视图(模型);" 那么原始查询字符串仍然存在?我需要在页面之间维护查询字符串.除了这个用例之外,我在任何地方都可以使用它.

谢谢!

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有