2012-12-04 46 views
0

客戶端驗證對我而言僅適用於外鍵。客戶端驗證不能與外鍵一起使用

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

列入以及在我的web.config文件

<add key="ClientValidationEnabled" value="true" /> 
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

集。

沒有自定義屬性或任何東西。這是簡單的東西

[Required(ErrorMessage = "Fiscal year is required")] 
[Display(Name = "Fiscal Year")] 
public int FiscalYearId { get; set; } 

對此有何想法?我已經搜索了一個解決方案,但是我發現的所有情況都不起作用,並且大多數解決方案都表示要確保上面的兩個腳本都來源,並且將上面的兩個鍵添加到web.conifg文件中。

我沒有得到js錯誤,我用螢火蟲監控。一切看起來都很完美,但它顯然不是。

一旦我更正了非外鍵字段並重新提交,數據驗證就可以在服務器端工作。這是一個問題,因爲它正在創建一個雙重驗證,並且讓用戶感到困惑。

非常感謝幫助。

以下是Html部分。它包含在一個使用塊中。這是標準的MVC3生成的CRUD代碼。

<div class="editor-label"> 
    <label for="FiscalYearId">Fiscal Year</label> 
</div> 
<div class="editor-field"> 
    <select id="FiscalYearId" name="FiscalYearId"> 
     <option value="">---Select Fiscal Year---</option> 
     <option value="1">FY2013</option> 
    </select> 
    <span class="field-validation-valid" data-valmsg-for="FiscalYearId" data-valmsg-replace="true"></span> 
</div> 
+0

你可以檢查併發布生成的HTML在FiscalYearId下拉?另外,確保你的表單元素在@using(Html.BeginForm()){}塊中。 –

+0

用html更新了我的問題 – Erik

+0

是的,選擇沒有驗證屬性,這不是javascript問題 – karaxuna

回答

0

我認爲在html中沒有呈現元數據屬性。試試這個在你的看法:

if (this.ViewContext.FormContext == null) 
{ 
    this.ViewContext.FormContext = new FormContext(); 
} 
+0

但是,我確實嘗試過,沒有運氣。 – Erik

+0

是您在Html.BeginForm中發佈的HTML嗎? – karaxuna

+0

是的,它發佈在Html.BeginForm中 – Erik