您好,这是我第一次使用 .NET MVC 3
我有一个控制器应该更改登录用户的密码:
public ActionResult ChangeUserPassword(string userId)
{
ChangePasswordModel model = new ChangePasswordModel() { Id = userId };
return View(model);
}
[HttpPost]
public ActionResult ChangeUserPassword(ChangePasswordModel model)
{
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather
// than return false in certain failure scenarios.
bool changePasswordSucceeded = true;
try
{
changePasswordSucceeded = userDetailsService.ChangeUserPassword(model.Id, model.OldPassword, model.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return View("ChangePasswordSuccess");
}
else
{
ViewBag.Message = "The current password is incorrect or the new password is invalid.";
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
}
// If we got this far, something failed, redisplay form
return View();
}
public ActionResult ChangePasswordSuccess()
{
return View();
}
视图如下:
@model MVCApp.Models.ChangePasswordModel
@{
ViewBag.Title = "Change User Password";
}
<h2>Change User Password</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Password</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.OldPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OldPassword)
@Html.ValidationMessageFor(model => model.OldPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NewPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
当我单击“保存”按钮时,什么也没有发生。如果我做错了什么或者如何捕捉和显示错误,你能建议我吗?
编辑:当我提交表单时,调试器没有发现任何错误
编辑:(图片因不相关而被删除)
我改变了控制器:
if (changePasswordSucceeded)
{
return View("ChangePasswordSuccess");
}
else
{
return View("ChangePasswordFailed");
}
但它不会将我重定向到任何地方......我真的不明白会发生什么
编辑:我认为模型是空的