2011-09-22 136 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不顯眼的驗證將處理您的自定義的驗證碼。