6

奇怪的事情不斷髮生在我的一個ASP.NET MVC 3應用程序上。未經驗證的部分視圖屬性(ASP.NET MVC 3)

我通過jQuery Ajax API獲取插入行,並沒有問題。但是,當我得到必要的部分視圖時,它會出現沒有驗證屬性的情況,我無法重新驗證這些行的驗證。

這裏是我所得到的Ajax響應:

<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="ccaa15b3-76f1-4215-8bb5-a62d700bfc1e" /> 
    <table style="width:100%;"> 
    <tr> 
     <td> 
      <div class="editor-field"> 
       <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option> 
<option value="303">B</option> 
<option value="304">C</option> 
<option value="305">D</option> 
</select> 

      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
       <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodStartsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodStartsAt" type="text" value="" /> 

      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
       <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodEndsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodEndsAt" type="text" value="" /> 

      </div> 
     </td> 
    </tr> 
    </table> 

這是我應該得到:

<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="84ddd0f5-a3e2-4f10-8e67-f32528c6393d" /> 
    <table style="width:100%;"> 
    <tr> 
     <td> 
      <div class="editor-field"> 
       <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." data-val="true" data-val-number="The field AccommPropertySeasonPeriodAliasID must be a number." data-val-required="The AccommPropertySeasonPeriodAliasID field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option> 
<option value="303">B</option> 
<option value="304">C</option> 
<option value="305">D</option> 
</select> 
       <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" data-valmsg-replace="false">*</span> 
      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
       <input class="datefield" data-val="true" data-val-required="The PeriodStartsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodStartsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodStartsAt" type="text" value="" /> 
       <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span> 
      </div> 
     </td> 
     <td> 
      <div class="editor-field"> 
       <input class="datefield" data-val="true" data-val-required="The PeriodEndsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodEndsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" type="text" value="" /> 
       <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span> 
      </div> 
     </td> 
    </tr> 
    </table> 

的GUID不必是相同的。我正在做所謂的非順序綁定。

這裏是哪,我通過jQuery的AJAX調用,以獲得新的插入行的動作:

[HttpPost] 
    public PartialViewResult accommPropertySeasonPeriodCreatePartialView(int id, int subid) { 

     //some other stuff going on here. non-related to partial view. 

     return PartialView("_AccommPropertySeasonPeriodCreatePartialView"); 
    } 

我幾乎從我的腦海弄清楚爲什麼發生這種情況。任何想法?

回答

10

Html.*幫助程序,如Html.TextBoxFor,Html.CheckBoxFor,......僅在用於表單內時才發送驗證屬性。所以請確保你將它們包裝在Html.BeginForm呼叫中。就客戶端驗證而言,在更新DOM以重新應用客戶端驗證之後,您應該調用jQuery.validator.unobtrusive.parse方法。和yet another article

如果你沒有,你可以欺騙一個形式,把在部分如下:

@model MyViewModel 
@{ 
    ViewContext.FormContext = new FormContext(); 
} 
@Html.EditorFor(x => x.Foo) 

現在助手會發出的輸入字段數據 - *驗證屬性。

+0

oww,這將是一個問題。我的主要觀點已經有了一個表格。任何其他方式來解決這個自動或者我應該重寫編輯器模板? – tugberk

+0

@tugberk,看到我的更新關於手動實例化部分中的FormContext。 –

+0

太棒了!完全合作。爲了理解它,這個調整究竟告訴框架(或模板引擎?),以便它理解在輸入字段上發出data- *驗證屬性? – tugberk