【问题标题】:"Remote" attribute doesn't work (ASP.net MVC)“远程”属性不起作用(ASP.net MVC)
【发布时间】:2021-08-18 07:43:04
【问题描述】:

我正在尝试使用 [Remote] 验证我的 NewPassword 属性,但是当其他属性的验证有效时,它不会在网页上显示任何消息错误。怎么了?

控制器动作

    public JsonResult VerifyNewPassword(ChangePasswordViewModel m)
    {
        return Json(false);

        //return Json(true);
    }

型号

    [Required(ErrorMessage = "New Password is required.")]
    [Remote("VerifyNewPassword", "AccountController", ErrorMessage="Error")]
    [DataType(DataType.Password)]
    public string NewPassword { get; set; }

【问题讨论】:

  • 您是否尝试过通过浏览器作为端点?有用吗?
  • 嗨,serhiy,答案是否解决了您的问题?如果是,请标记答案以帮助未来的用户。谢谢

标签: asp.net validation data-annotations


【解决方案1】:

错误可能在Remote 数据注释中,因为您已使用全名指定控制器,但您只需要第一部分。

试试这个:

[Required(ErrorMessage = "New Password is required.")]
[Remote("VerifyNewPassword", "Account", ErrorMessage="Error")]
...

并注意方法的参数,应该是这样的:

public IActionResult VerifyNewPassword(string newPassword)
{
    if (!_userService.VerifyPassword(newPassword))
    {
        return Json($"Password does not respect security standard.");
    }

    return Json(true);
}

【讨论】:

  • 我已经尝试过这样做,但它对我也没有帮助。
  • 我在这个动作方法上设置了一个断点,但是程序甚至没有使用它。
  • 你能分享你的控制器代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
相关资源
最近更新 更多