0

在使用 EditorForModel 字段以及验证和 ModelState.IsValid 时,如何返回原始视图但保留填充的字段?

调试时,我可以看到我传回视图的模型中有字段数据,但文本输入等不包含这些值。

我究竟做错了什么?

控制器代码如下:

[Authorize]
public ActionResult ChangePassword()
{
  return View(new Views.PublicAuthChangePassword());
}

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("ChangePassword")]
public ActionResult ChangePassword_Post(ChangePasswordVM vm)
{
  try
  {
      if (ModelState.IsValid)
      {
        MyUser user = new MyUser();
        bool changeResponse = MyUser.ChangePassword(vm.OldPassword, vm.NewPassword);                    

        if (changeResponse)
          ViewBag.Message = "Password changed successfully";
        else
          ViewBag.Message = "Unable to change password";
      }
    }
    catch (System.ArgumentNullException ex)
    {
      //Either old password or new password are null
    }
    catch (System.ArgumentException ex)
    {
      //Either old password or new password are empty string
    }
    catch (PlatformNotSupportedException ex)
    {
      //This method is not available. 
    }
    catch (Exception ex)
    {
      //An unknown error occurred
    }
  return View(vm);
}

看法:

@model ChangePasswordVM

@using (Html.BeginForm("ChangePassword", "MyController", FormMethod.Post, new { @class = "change-password" }))
{
  <h2>Change My Password</h2>
  @Html.AntiForgeryToken()
  @Html.EditorForModel(Model)
  <input id="btnChangePassword" type="submit" value="Change password" />
}

模型:

public class ChangePasswordVM 
{
  [Required(ErrorMessage = "Old password is required")]
  [DataType(DataType.Password)]
  public string OldPassword { get; set; }

  [Required(ErrorMessage = "New password is required")]
  [DataType(DataType.Password)]
  public string NewPassword { get; set; }

  [Required(ErrorMessage = "Confirm password is required")]
  [DataType(DataType.Password)]
  [CompareAttribute("NewPassword", ErrorMessage = "Password doesn't match.")]
  public string ConfirmPassword { get; set; }
}

谢谢西蒙

4

1 回答 1

0

我会翻转你的模型状态检查。

public ActionResult ChangePassword_Post(PasswordViewModel vm)
{
   if (!ModelState.IsValid)
   {
      return View(vm);
   }
   try
   {
      MyUser user = new MyUser();
      bool changeResponse = MyUser.ChangePassword(vm.OldPassword, vm.NewPassword);                    

      if (changeResponse)
        ViewBag.Message = "Password changed successfully";
      else
        ViewBag.Message = "Unable to change password";

    }
    catch (System.ArgumentNullException ex)
    {
      //Either old password or new password are null
    }
    catch (System.ArgumentException ex)
    {
      //Either old password or new password are empty string
    }
    catch (PlatformNotSupportedException ex)
    {
      //This method is not available. 
    }
    catch (Exception ex)
    {
      //An unknown error occurred
    }

    ViewBag.Message = "Unable to change password";
    return View(vm);
}

如果您的模型状态检查失败,这会将您的模型验证错误返回到您的视图。它还将使用您的视图模型值重新填充任何字段。

如果您有任何下拉菜单,则需要显式地重新创建数据并将其传回。

您还必须在最后再次设置您的 ViewBag.Message ,因为只有在调用失败且未引发异常时才设置它。

您还应该遵循 PRG 模式并重定向到显示或索引页面,而不是同一个视图。

于 2018-07-16T15:09:01.323 回答