2014-03-25 88 views
2

可能這可能是一些更類似於其他問題但我還沒有找到我想要的解決方案。顯示Hidden字段的驗證消息

我想表明我在MVC設定特定領域的驗證消息::

我的視圖模型是::

public class MainDocumentViewModel 
    { 


     [Required(ErrorMessage = "Please Enter Valid Customer Name")] 
     public long CustomerID { get; set; } 
    } 

我的觀點是爲::

<form> 

    @Html.HiddenFor(x => x.CustomerID) 
    @Html.ValidationMessageFor(x=>x.CustomerID) 

<button type="submit">Submit</button> 
</form> 

但提交表單後,我沒有收到任何錯誤。 幫我解決這個問題。 在此先感謝。

+0

你想客戶端'不顯眼的JavaScript validation'?或服務器端? –

+0

我已經定義到我的視圖模型的服務器端 – Rahul

回答

3

請通過Here,這是非常有幫助的

客戶端驗證隱藏字段不工作,因爲jQuery驗證忽略所有隱藏的標籤。

您必須定義HiddenRequiredValidator類來實現您的目標。

2

如果您默認使用jQuery驗證插件,它將忽略hidden字段。要驗證隱藏字段,請更改默認設置

$.validator.setDefaults({ 
    ignore: [], 
    // other default options 
}); 
2

僅用於驗證特定隱藏字段,請執行此操作。

<script> 
    $.validator.setDefaults({ 
     ignore: $('#CustomerID') 
    }); 
</script> 

或特定形式

$('#myform').validate({ 
     ignore: [] 
    }); 
+0

這將忽略ID爲'CustomerID'的驗證:) –