2015-10-09 50 views
1

我已經把模型類中的mvc遠程驗證,並且這個遠程驗證方法正在調用哪個控制器。它完全適用於mvc視圖,但不適用於kendo ui gridKendo UI Grid不支持遠程MVC驗證?

如果有人知道這個問題,請幫助我。

我的模型代碼是:

[Required] 
    [Remote("CheckDuplicateRoleName", 
     "Role", 
     AdditionalFields = "RoleId", 
     ErrorMessage = "This Role Name already exists.")] 
    public string RoleName { get; set; } 

    }  

MVC的遠程驗證CheckDuplicateRoleName是我已經把在Role controllerRoleId方法名稱爲附加字段。我希望存在於劍道彈出窗口顯示該errormessage用於檢查重複角色名在數據庫中。但這種控制器方法沒有調用。用於遠程驗證MVC

回答

0

我已經發現溶液

(function ($, kendo) { 
     $.extend(true, kendo.ui.validator, { 
      rules: { 
       mvcremotevalidation: function (input) { 
        if (input.is("[data-val-remote]") && input.val() != "") { 
         var remoteURL = input.attr("data-val-remote-url"); 
         var valid; 

         $.ajax({ 
          async: false, 
          url: remoteURL, 
          type: "GET", 
          dataType: "json", 
          data: validationData(input, this.element), 
          success: function (result) { 
           valid = result; 
          }, 
          error: function() { 
           valid = false; 
          } 
         }); 

         return valid; 
        } 

        return true; 
       } 
      }, 
      messages: { 
       mvcremotevalidation: function (input) { 
        return input.attr("data-val-remote"); 
       } 
      } 
     }); 

     function validationData(input, context) { 
      var fields = input.attr("data-val-remote-additionalFields").split(","); 
      var name = input.prop("name"); 
      var prefix = name.substr(0, name.lastIndexOf(".") + 1); 
      var fieldName; 
      var data = {}; 
      for (var i = 0; i < fields.length; i++) { 
       fieldName = fields[i].replace("*.", prefix); 
       data[fieldName] = $("[name='" + fieldName + "']", context).val(); 
      } 
      return data; 
     } 
    })(jQuery, kendo);