2011-10-06 100 views
0

我想通過ajax添加和刪除國家對象的本地化名稱。因此我已經構建了兩個部分視圖。第一個包含country-object的generel edit funcionality,第二個partial視圖(將在第一個視圖內呈現)包含添加/刪除本地化名稱的邏輯。首次提交Ajax Form作品,但第二次提交不做任何事

第一局部視圖:

@model CountryViewModel 

// scripts here 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 
    <fieldset> 
    [...] // the fields of the object to edit... 
    </fieldset> 
} 

// this tag will be updated by the partial view 
<div id="localizedNamesOverview"> 

@Html.Partial("LocalizedNamesOverview", 
       Model.LocalizedNames, 
       new ViewDataDictionary 
       { 
       { "countryId", Model.CountryId } 
       }) 

</div> 

第二部分視圖:

@model IEnumerable<LocalizedNameViewModel> 

<table> 

@foreach (var item in Model) 
{ 
    <tr> 
     <td> @item.Language </td> 
     <td> @item.Name </td> 
     <td> 
      @Ajax.ActionLink("Delete", 
           "DeleteLocalizedName", 
           "Country", 
           new { countryId = this.ViewData[ "countryId" ], 
            localizedNameId = item.CountryI18nId }, 
           new AjaxOptions 
           { 
           UpdateTargetId="localizedNamesOverview", 
           InsertionMode=InsertionMode.Replace, 
           HttpMethod="POST" 
           }) 
     </td> 
    </tr> 
} 

@using(Ajax.BeginForm("AddLocalizedName", 
         "Country", 
         new { countryId = this.ViewData[ "countryId" ] }, 
         new AjaxOptions 
         { 
          UpdateTargetId = "localizedNamesOverview", 
          InsertionMode = InsertionMode.Replace, 
          HttpMethod = "POST" 
         })) 
{ 
    <tr> 
     <td> <input class="text-box single-line" id="LanguageId" name="LanguageId" value="" type="text" /> </td> 
     <td> <input class="text-box single-line" id="Name" name="Name" value="" type="text" /> </td> 
     <td> <input type="submit" value="Add new localized name" /> </td> 
    </tr> 
} 

</table> 

專用控制器返回時本地化名稱要麼添加或移除的第二局部視圖,並通過添加內容替換其本身從第一個視圖進入'localizedNamesOverview'。到目前爲止,這工作正如我所料。

現在的問題是這種行爲只有一次。如果我已成功添加或刪除名稱,則無法刪除/添加第二個名稱。目前我看不到問題出在哪裏,因爲生成的html看起來等​​於第一次提交之後。

任何幫助表示讚賞。
由於

回答

0

好,我想出了什麼問題:

當第一請求是由與所述第二部分視圖重新渲染它從其中需要與母體失去ViewData的信息(countryId)生成工作鏈接(每個現有本地化名稱的刪除鏈接)。

同樣的問題適用於也會丟失id的sourrounding ajax-form。通過重新定位代碼,我可以解決這個問題。我將@using(Ajax.BeginForm...代碼從第二部分視圖放到父視圖中,以便在ajax請求之後不會重新生成代碼。

1日局部視圖(更新):

@using(Ajax.BeginForm("AddLocalizedName", 
         "Country", 
         new { countryId = this.ViewData[ "countryId" ] }, 
         new AjaxOptions 
         { 
          UpdateTargetId = "localizedNamesOverview", 
          InsertionMode = InsertionMode.Replace, 
          HttpMethod = "POST" 
         })) 
{ 

// this tag will be updated by the partial view 
<div id="localizedNamesOverview"> 

@Html.Partial("LocalizedNamesOverview", 
       Model.LocalizedNames, 
       new ViewDataDictionary 
       { 
        { "countryId", Model.CountryId } 
       }) 

</div> 

} 

的刪除鏈接問題可以解決,因爲我的模型LocalizedNameViewModel還包含專用countryId,所以我把它從那裏。

第二局部視圖(更新):

@Ajax.ActionLink("Delete", 
        "DeleteLocalizedName", 
        "Country", 
        new { countryId = item.CountryId, // took countryId from the model 
         localizedNameId = item.CountryI18nId }, 
        new AjaxOptions 
         { 
         UpdateTargetId="localizedNamesOverview", 
         InsertionMode=InsertionMode.Replace, 
         HttpMethod="POST" 
         }) 
相關問題