2012-04-22 27 views
1

我面臨我的asp.net mvc3應用程序內部的一般問題,即所有局部視圖都不會顯示我使用數據註釋定義的客戶端驗證錯誤 ,, for例如我已經定義了以下數據的註解: -客戶端數據註解不適用於局部視圖

public class Country_Validation 
    { 
     [Required(ErrorMessage="{0} is required.")] 
     [StringLength(30, ErrorMessage="{0} is too long.",MinimumLength=1)] 
     public string Description { get; set; } 
    } 

和我有以下動作的方法將使用ajax.beginform被調用,這將隨後返回的局部視圖來創建新Country對象: -

public ActionResult Create() 
     { 
      Country c = new Country(); 
      return PartialView("_Create",c); 
     } 

這將返回以下_Create局部視圖: -

@model Medical.Models.Country 

<div id = "partialWrapper"> 
@using (Ajax.BeginForm("Create", "Country", new AjaxOptions 
{ 
    HttpMethod = "Post", 
    InsertionMode = InsertionMode.InsertBefore, 
    UpdateTargetId = "Countrytable", 
    OnSuccess = "clearform" 
})) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Country</legend> 
     <div class="editor-label"> 
     <strong>Country Name:-</strong> 
      @Html.EditorFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 
      <input type="submit" value="Create" /> 
    </fieldset> 
} 
</div> 

但如果我嘗試同時留下Description字段爲空以添加新country對象,然後將顯示沒有客戶端驗證錯誤,而如果我返回一個常規的視圖(不是部分),那麼所有的客戶端驗證錯誤將正常工作? 那麼可能是什麼問題? BR

回答

3

我最近碰到類似的東西。該解決方案是我的內容前右以下內容添加到我的局部視圖(S)(但之後,我宣佈我的模型類型和任何使用說明):

@{ 
    ViewContext.FormContext = new FormContext(); 
} 

你也可能想看看下面的鏈接與實現驗證了新添加的內容的JavaScript方交易:

http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/

+0

我回到了這個問題,但增加iewContext.FormContext =新FormContext();不管用。 – 2014-01-15 14:00:27

相關問題