Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ASP.NET MVC2和jQuery进行远程验证_C#_Jquery_Validation_Asp.net Mvc 2 - Fatal编程技术网

C# 使用ASP.NET MVC2和jQuery进行远程验证

C# 使用ASP.NET MVC2和jQuery进行远程验证,c#,jquery,validation,asp.net-mvc-2,C#,Jquery,Validation,Asp.net Mvc 2,我正在尝试实现远程客户端验证,以检查是否已使用用户名。我阅读了一篇博文和一篇文章,并提出了以下实施方案: public class RemoteAttribute : ValidationAttribute { public string Action { get; set; } public string Controller { get; set; } public override bool IsValid(object va

我正在尝试实现远程客户端验证,以检查是否已使用用户名。我阅读了一篇博文和一篇文章,并提出了以下实施方案:

   public class RemoteAttribute : ValidationAttribute
    {
        public string Action { get; set; }
        public string Controller { get; set; }

        public override bool IsValid(object value)
        {
            return true;
        }
    }


public class RemoteValidator : DataAnnotationsModelValidator<RemoteAttribute>
{

    public RemoteValidator(ModelMetadata metadata, ControllerContext context, RemoteAttribute validationAttribute) :
        base(metadata, context, validationAttribute)
    {
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
                                             {
            ErrorMessage = Messages.DuplicateUsername,
            ValidationType = "remote"
        };

        rule.ValidationParameters.Add("url", Attribute.Controller + "/" + Attribute.Action);
        return new[] { rule };
    }
}
我使用jquery的验证方法如下:

   [Remote(Controller = "SignUp",Action = "IsUsernameAvailable")]
   public string Username { get; set; }
jQuery.validator.addMethod("remote", function (value, element, params) {
    if (this.optional(element)) {
        return true;
    }

    if (value != '') {
        $.post(params.url, { username: value }, function (response) {
            return response;
        });
    }

});
  public JsonResult IsUsernameAvailable(string userName)
    {
        var isUsernameAvailable = _userService.IsUsernameAvailable(userName);
        if (isUsernameAvailable)
        {
            return Json(true);
        }

        return Json(false);
    }
在我的控制器中,我有一些操作方法,如下所示:

   [Remote(Controller = "SignUp",Action = "IsUsernameAvailable")]
   public string Username { get; set; }
jQuery.validator.addMethod("remote", function (value, element, params) {
    if (this.optional(element)) {
        return true;
    }

    if (value != '') {
        $.post(params.url, { username: value }, function (response) {
            return response;
        });
    }

});
  public JsonResult IsUsernameAvailable(string userName)
    {
        var isUsernameAvailable = _userService.IsUsernameAvailable(userName);
        if (isUsernameAvailable)
        {
            return Json(true);
        }

        return Json(false);
    }

出于某些原因,即使我的actiom方法IsUsernameAvailable返回true,也会始终显示验证消息。我做错了什么?

您的调用RemoteAttribute.IsValid()总是返回true。添加您的IsUsernameAvailable检查,并根据需要返回true或false

IsValid()函数告诉模型是否触发错误。

尝试发送字符串“true”和“false”,而不是带有

return Content("true") 


尝试Json(true,JsonRequestBehavior.AllowGet);或Json(false,JsonRequestBehavior.AllowGet)

如果您的IsUsernameAvailable()调用正确返回真/假?另外,请发布控制器代码。是的,它返回了正确的值。我已经发布了操作方法的代码。我想使用远程属性。。从你的ans中,我有了一个想法,但你能告诉我……微软的MVC2中有吗?