在unbtrusive适配器中使用jQuery .ajax进行验证。

时间:2021-03-04 22:12:26

I have an unobtrusive validation defined on a class which validates that the number entered is a valid number in the database. I'd prefer not to use a Remote attribute, as it ties me to a specific controller instead of a service, and the class is actually in a library that is not in the MVC Web application. I'd like to use JavaScript as follows:

我在一个类上定义了一个不显眼的验证,该验证输入的数字是数据库中的一个有效数字。我不喜欢使用Remote属性,因为它将我与一个特定的控制器联系在一起,而不是一个服务,而且这个类实际上位于一个不在MVC Web应用程序中的库中。我想使用JavaScript如下:

$.validator.addMethod("validid", function (value, element) { 

    $.ajax({
      url: 'mypage.html',
      success: function(){
        return true;
      },
      error: function(){
        return false;
      }
    });
}); 

$.validator.unobtrusive.adapters.addBool("validid"); 

I know that I can use an addsingleval and pass in all of the valid IDs in an array, but I would prefer the method to call a service and validate the sing value on the server instead of passing in all possible values in the database. I do recognize the problem of making an asynchronous call and trying to return the value in the function that has already completed, but I'm wondering if there's some other way to make this work.

我知道我可以使用addsingleval函数并在数组中传递所有有效的id,但是我更希望该方法调用服务并在服务器上验证sing值,而不是在数据库中传递所有可能的值。我确实认识到进行异步调用并试图返回已经完成的函数中的值的问题,但我想知道是否有其他方法可以实现这一点。

2 个解决方案

#1


1  

I'd prefer not to use a Remote attribute, as it ties me to a specific controller instead of a service, and the class is actually in a library that is not in the MVC Web application.

我不喜欢使用Remote属性,因为它将我与一个特定的控制器联系在一起,而不是一个服务,而且这个类实际上位于一个不在MVC Web应用程序中的库中。

If I understand your problem, what prevents you from using the Asp.Net Mvc Remote attribute is that you are only offered routes when you want to specify an arbitrary url.

如果我理解您的问题,是什么阻止您使用Asp。Net Mvc Remote属性是,只有在指定任意url时才提供路由。

Maybe you can try with a custom override of the Remote attribute

也许您可以尝试使用远程属性的自定义覆盖

public class CustomRemote : RemoteAttribute
{
    protected String url { get; set; }

    public CustomRemote(string url)
    {
        this.url = url;
    }

    protected override string GetUrl(ControllerContext controllerContext)
    {
        return url;
    }
}

Then, in your view model :

然后,在你的视图模型中:

[CustomRemote("mypage.html")]
public String Id

#2


0  

While the answer @jbl provided would work, we are trying to keep the validation logic in a separate class, and would prefer not to include the MVC library. The solution ended up being relatively simple, using an async: false in the method. We ended up with:

虽然提供的答案@jbl可以工作,但是我们试图将验证逻辑保存在一个单独的类中,并且不希望包含MVC库。该解决方案最终变得相对简单,方法中使用了async: false。我们结束了:

$.validator.addMethod("validid", function (value, element) { 

    var isValid = false;

    $.ajax({
      url: 'mypage.html',
      async: false,
      success: function(data){
        isValid = data;
      },
      error: function(){
        isValid = false;
      }
    });

    return isValid;
}); 

$.validator.unobtrusive.adapters.addBool("validid"); 

#1


1  

I'd prefer not to use a Remote attribute, as it ties me to a specific controller instead of a service, and the class is actually in a library that is not in the MVC Web application.

我不喜欢使用Remote属性,因为它将我与一个特定的控制器联系在一起,而不是一个服务,而且这个类实际上位于一个不在MVC Web应用程序中的库中。

If I understand your problem, what prevents you from using the Asp.Net Mvc Remote attribute is that you are only offered routes when you want to specify an arbitrary url.

如果我理解您的问题,是什么阻止您使用Asp。Net Mvc Remote属性是,只有在指定任意url时才提供路由。

Maybe you can try with a custom override of the Remote attribute

也许您可以尝试使用远程属性的自定义覆盖

public class CustomRemote : RemoteAttribute
{
    protected String url { get; set; }

    public CustomRemote(string url)
    {
        this.url = url;
    }

    protected override string GetUrl(ControllerContext controllerContext)
    {
        return url;
    }
}

Then, in your view model :

然后,在你的视图模型中:

[CustomRemote("mypage.html")]
public String Id

#2


0  

While the answer @jbl provided would work, we are trying to keep the validation logic in a separate class, and would prefer not to include the MVC library. The solution ended up being relatively simple, using an async: false in the method. We ended up with:

虽然提供的答案@jbl可以工作,但是我们试图将验证逻辑保存在一个单独的类中,并且不希望包含MVC库。该解决方案最终变得相对简单,方法中使用了async: false。我们结束了:

$.validator.addMethod("validid", function (value, element) { 

    var isValid = false;

    $.ajax({
      url: 'mypage.html',
      async: false,
      success: function(data){
        isValid = data;
      },
      error: function(){
        isValid = false;
      }
    });

    return isValid;
}); 

$.validator.unobtrusive.adapters.addBool("validid");