2014-02-27 23 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