2011-09-22 104 views
0

我正在从asp.net mvc 2将网站转换为asp.net mvc 3.我想使用mvc 3中的内置验证,它使用jquery.validate和jquery.validate.unobtrusive。然而,在我的旧网站上,我已经使用jquery.validate并添加了自定义方法进行验证,然后在下拉更改时再调用。自定义asp.net mvc 3 jquery.validate.unobtrusive

我需要能够:

  1. 注册此方法。
  2. 只有当下拉改变时才会呼叫。

这是我在我的asp.net 2网站上的代码。

//add class to the html element 
$("#ClientId-input").addClass("validClient"); 

//create the method "validClient" 
$.validator.addMethod("validClient", function(value, element) { 

    //get the actual value in the input box 
    var _value = $("#ClientId").data('tComboBox').value(); 

    //get all the items in the list 
    var _items = $("#ClientId").data('tComboBox').data; 

    //set the value to return 
    var _retVal = false; 

    //loop through the items if the selected value equals a value we are valid 
    $.each(_items, function(index, value) 
    { 
     if(value.Value == _value){ 
      _retVal = true; 

      //return false in the loop to break. 
      return false; 
     } 
    }); 

    return _retVal; 

}, "Please choose a Client from the list."); 


//Assign the rule to the validator 
$.validator.addClassRules({ 
    validClient:{validClient:true} 
}); 


//this is called when dropdownchanges 
function ClientsChanged(e) 
{ 

    if($("#ClientId-input").valid()) 
    { 
     //do work here 
    } 
} 

回答

0

在我的答案here,在底部,你会看到一个显示如何添加和注册自定义的验证方法,使MVC 3不显眼的验证将处理您的自定义的验证码。