The MVC message causes the QueryString to be lost upon reloading of the same kind

Please let me explain the settings.

I have a Control Password Controller / Action and View. Here are the action signatures in my account controller:

public ActionResult ChangePassword(ChangePasswordMessageId? message)

[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)

When the first change password is loaded, I have some data in the query string. Here is an example:

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

Here is the form declaration from the submission.

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

The form is submitted with a simple submit button:

<div class="form-group">
  <div class="col-md-offset-2 col-md-4">
    <input type="submit" value="Change Password" class="btn btn-primary btn-block" />
  </div>
</div>

The form has 3 fields: current password, new password and password confirmation. If the user correctly fills in all the data and passes all the checks on the client side, the form works fine. Everything works fine except for one use case.

, . 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);
            }

, "return View (model)"; ? . , .

!

+4
6

- , .

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

- Mobile . , Get, , Mobile View Model.

, . :.

@Html.Hidden(Model.Mobile)

Mobile ChangePasswordViewModel, .

+1
return RedirectToAction("Index", Request.QueryString.ToRouteValues());
0

VB, URL :

@Functions
Public Function GetRouteValues() As RouteValueDictionary
    Dim RouteValues As New RouteValueDictionary
    For Each Qstr As String In Request.QueryString
        RouteValues.Add(Qstr, Request.QueryString.GetValues(Qstr).FirstOrDefault)
    Next
    Return RouteValues
End Function
End Functions

@Using (Html.BeginForm("MultipleHandler", "Files", GetRouteValues, FormMethod.Post))

, .

:

Private Function AllRouteVlaues() As RouteValueDictionary
        Dim RouteValues As New RouteValueDictionary
        For Each Qstr As String In Request.QueryString
            RouteValues.Add(Qstr, Request.QueryString.GetValues(Qstr).FirstOrDefault)
        Next
        Return RouteValues
    End Function

:

Return RedirectToAction("Index", AllRouteVlaues)

, :)

0

:

using (Html.BeginForm("Action", "Controller",  new { Var1 = (string)ViewBag.Var1, Var2 = (string)ViewBag.Var2, Var3 = (string)ViewBag.Var3 }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                    {
0

You need to specify the query string parameters in the new query "somehow".
On the way there would be the following (this is also how MS does this with ReturnUrlin its examples):

public ActionResult ChangePassword(ChangePasswordMessageId? message. int mobile = 0)
{
    // your code
    ViewBag.Mobile = mobile;
    return View();
}
[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model, int mobile = 0)
{
    // your code
    ViewBag.Mobile = mobile;
    return View(model);
}

and

@using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, 
    new { @class = "form-horizontal", role = "form", mobile = @ViewBag.Mobile }))
-1
source

All Articles