ASP.NET MVC:远程验证未触发

时间:2015-11-25 22:49:50

标签: c# model-view-controller remote-validation

调用远程验证时出现问题。我在控制器的远程方法中使用匹配的属性名称和参数进行了正确设置。我唯一怀疑的是我在一个单独项目中的模型。

这是我的模型代码:

[Display(Name="First name")]
    [Required(ErrorMessage="First name required")]
    public string Firstname { get; set; }

    [Display(Name = "Last name")]
    [Required(ErrorMessage = "Last name required")]
    public string Lastname { get; set; }

    [Display(Name = "Username")]
    [Required(ErrorMessage = "Username required")]
    [Remote("IsUsernameExisted", "UserValidationController", HttpMethod = "POST")]
    public string Username { get; set; }

    [Display(Name = "Email address")]
    [Required(ErrorMessage = "Email address required")]
    [Remote("IsEmailExisted", "UserValidationController", HttpMethod = "POST")]
    public string EmailAddress { get; set; }

    [Display(Name = "Password")]
    [Required(ErrorMessage = "Password required")]
    public string Password { get; set; }

    [Display(Name = "Confirmation password")]
    [Required(ErrorMessage = "Confirmation required")]
    [System.Web.Mvc.CompareAttribute("Password", ErrorMessage = "Passwords don't match.")]
    public string ConfirmationPassword { get; set; }

这个模型是一个单独的项目,但在visual studio中使用相同的解决方案。

这是远程验证未被解雇的原因吗?

1 个答案:

答案 0 :(得分:0)

你的web.config可能会遗漏这个吗?

  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

&#34; 因为当您使用不显眼的客户端验证时不会发出JavaScript,如果您忘记包含验证脚本,则在页面加载时您将看不到任何错误。唯一的结果是表单值不会在浏览器中验证。&#34;

来源:How to: Implement Remote Validation in ASP.NET MVC