【问题标题】:Custom asp.net mvc 3 jquery.validate.unobtrusive自定义 asp.net mvc 3 jquery.validate.unobtrusive
【发布时间】:2011-11-23 02:00:13
【问题描述】:

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

我需要能够:

  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
    }
}

【问题讨论】:

    标签: jquery asp.net asp.net-mvc-3 validation


    【解决方案1】:

    在我的回答 here 的底部,您将看到显示如何添加和注册自定义验证方法的代码,以便 MVC 3 不显眼的验证将处理您的自定义验证器。

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多