如何在MVC表单发布后删除URL中的操作名称

时间:2017-04-06 22:59:39

标签: asp.net-mvc

我有一个重置密码方法,我在MVC中发布它。当我点击提交按钮并发布页面时,网址就像:http://localhost:11580/resetpassword 所以它会在URL中带来action方法的名称,这就是为什么页面中的链接不再起作用了,我可以摆脱resetpassword动作名称吗?

我所指的链接是我的索引页面中的链接:

@Html.ActionLink("Forgot Password", "forgotPassword", new { controller = "Home" } , new { id = "btn-forgot-password"})

因为所有这些代码都在Modal中,而不是索引页面。

我正在使用MVC表单帖子,这是我的方法:

       [HttpGet]
       [Route("resetpassword")]
       [AllowAnonymous]
       public async Task<ActionResult> ResetPassword(ResetPasswordRequest resetPasswordRequest)
          {
            .....
                        return View("ForgotPasswordError");
                }
                else
                {
                  .......
                    return View("Index", logInRequest);
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
        return View("Index", logInRequest);
    }

    [HttpPost]
    [Route("resetpassword")]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel resetPasswordViewModel)
    {
        ....
        if (ModelState.IsValid)
        {
            ......
                }
            }
            else
            {
                ModelState.AddModelError("", "Please enter the same value    again.");
                  return View(resetPasswordViewModel);
            }
        }
        else
        {
            ModelState.AddModelError("", "Password is Required Field.");
            return View(resetPasswordViewModel);
        }
        LogInRequest logInRequest = new LogInRequest();
        logInRequest.IsThankYouPage = true;
        return View("Index", logInRequest);
    }

这是发布的视图:

  @model Models.LogInRequest
   <link href="~/Content/Styles/ModalStyle.css" rel="stylesheet" />
   <link href="~/Content/Styles/style.css" rel="stylesheet" />
   @{
   Layout = "~/Views/Shared/MasterLayout.cshtml";
    }
   @if (Model != null && Model.IsResetPassword)
  {
  <div class="page resetPassword">
    @using (Html.BeginForm("resetpassword", "Home", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        <div class="modal">
            <div class="modal-content">
                <span class="close">X</span>
                <h2 id="modal-title">Reset Your Garage Password</h2>
                <hr />
                <div>
                    <div class="create-user-label">Email Address</div>
                    <div><input type="email" name="Email"   placeholder="jdoe@example.com" class="create-new-password-textbox-grey" readonly value=@Model.UserName /></div>
                    <div class="create-user-label">Password</div>
                    <div><input type="Password" id="Password" name="Password" placeholder="Create Password" class="create-new-password-textbox" onblur="validatePassword();" /></div>
                    <div id="resetPassword-default-message" class="resetPassword-default-message">Password must be at least 8 characters.</div>
                    <div id="resetpassword-message"><label class="resetpassword-error" id="password-error"></label> </div>
                    <div class="create-user-label">Confirm Password</div>
                    <div><input type="Password" name="ConfirmPassword" id="ConfirmPassword" placeholder="Re-enter Password" class="create-new-password-textbox" onblur="validateConfirmPassword();" /></div>
                    <div id="confirmPassword-message"><label class="resetpassword-error" id="confirmPassword-error"></label> </div>
                    <br />
                    <div>
                        <input type="submit" id="btnSubmit" value="Submit" class="orange-button create-password-button-half" onclick="validatePasswordSubmit();" />
                        <input type="hidden" value="@Model.Token" name="token" />
                    </div>
                </div>
            </div>
        </div>
    }
</div>
   }
  @if(Model.IsThankYouPage)
  {
<div id="ThankYou-Page" class="modal">
<!-- Modal content -->
<div class="modal-content">
    <div class="close">x</div>
    <div id="reset">
        <div class="modal-heading">Success!</div>
        <hr class="forgot-password-line" />
        <br />
        <p>Your password has been successfully reset. Please sign in.</p>
        <br />
    </div>
</div>
 </div>
  <script type="text/javascript">
  renderThankYouPage();
  </script>
 }
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
 <script src="~/Scripts/User/CreatePassword.js"></script>

1 个答案:

答案 0 :(得分:0)

这是表单发布到的URL,并且由于服务器的响应是一个视图,然后是用户看到的内容。

不是返回视图,而是将用户重定向到您想要的操作。所以不要这样:

return View("Index", logInRequest);

这样的事情:

return RedirectToAction("Index");

我想这里真正的问题是,logInRequest是什么?为什么索引视图需要这个?理想情况下,您应该在这里做的是重定向,并且确实不应该传递任何数据。用户已重置密码,他们应该可以继续。