Bob*_*tiz 6 c# asp.net asp.net-mvc post asp.net-mvc-4
请让我解释一下设置.
我有一个更改密码控制器/操作和视图.以下是我的帐户控制器中的操作签名:
public ActionResult ChangePassword(ChangePasswordMessageId? message)
[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)
Run Code Online (Sandbox Code Playgroud)
首次加载更改密码时,我在查询字符串中有一些数据.这是一个例子:
https://www.mywebsite.com/Account/ChangePassword?mobile=1
这是视图中的Form声明.
@using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
表单通过简单的提交按钮提交:
<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>
Run Code Online (Sandbox Code Playgroud)
表单有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);
}
Run Code Online (Sandbox Code Playgroud)
是否可以称之为"返回视图(模型);" 那么原始查询字符串仍然存在?我需要在页面之间维护查询字符串.除了这个用例之外,我在任何地方都可以使用它.
谢谢!
如果有人仍在寻找此解决方案,请尝试不提供控制器和操作名称.
@using (Html.BeginForm(null, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6412 次 |
| 最近记录: |