[英]Jquery Validation MVC3 asp.net not showing Validation msg
我的模特上有這個
[Required]
public string AppName { get; set; }
在我的布局頁面上
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.24.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
my view has this inside BeginForm
@Html.EditorFor(model => model.App.AppName, new { size = 35 })
@Html.ValidationMessageFor(model => model.App.AppName)
<input type="submit" value="Start Business" class="demo-button ui-state-default ui-corner-all" />
this is how its rendered
<input id="App_AppName" class="text-box single-line watermark" type="text" value="" name="App.AppName" data-val-required="The AppName field is required." data-val="true">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="App.AppName"></span>
當我點擊提交時,我沒有得到任何驗證,說AppName是必需的,但它只是回發到具有空字段的服務器。
我添加了幾個字段作為validationMsgFor隱藏的字段,並且可以正常工作。
所以我有的是
public class AppContent
{
public App App { get; set; }
public bool CreateModal { get; set; }
public AppContent()
{
}
}
我的視圖是AppContent類型。
我注意到的一件事是,當我只是直接添加帶有App對象的視圖時,就會根據需要檢測到AppName。 但是當我使用類型為AppName的AppCOntent進行驗證時,不會顯示驗證,但對於非字符串的其他隱藏字段,它可以工作。
您必須在web.config中啟用客戶端驗證。
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
你必須把這個腳本
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
因為我有水印,我不得不使用占位符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.