2014-02-27 22 views
0

爲MVC中的隱藏字段遠程驗證不會被解僱 型號:應用遠程驗證的隱藏字段中mvc4

[Remote("checker", "mycontroller", ErrorMessage = "Valid combination of phone and account number required.", HttpMethod = "Get")] 
     public string Validate_cart { get; set; } 

查看:

@Html.HiddenFor(model => model.Validate_Paris) 

也試過通過使用jquery設置值:

$("#Phone_Number").blur(function() { 
$("#Validate_cart").val = "dummy" 
}); 

使用jquery或通過模型的值設置,但驗證不會被解僱。我使用fiddler進行了檢查,沒有任何時間調用該方法的呼叫。

方法

[HttpGet] 
     public bool checker(string Validate_cart) 
     { 
      try 
      { 

       bool isValid = //code to hit database to check the record 
       return !isValid;    

      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

回答

0

隱藏字段由jQuery驗證默認被忽略掉。這是由於以下設置。

$("form").validate().settings.ignore 

設置爲「:hidden」,以便驗證忽略所有隱藏字段。你可以做的是改變分配給忽略的選擇器,如下所示。

$(function(){ 
    $("form").validate().settings.ignore = ":hidden:not([id*=Validate_cart])"; 
}); 

那麼就應該解僱遠程驗證

+0

它仍然不火的驗證。 – Nikitesh

+0

你能否用'checker'' action'代碼更新你的文章? – Amila