第一次提交 Ajax 表单有效,但第二次提交没有任何作用

发布于 2024-12-08 09:15:04 字数 2591 浏览 0 评论 0原文

我想通过 ajax 添加和删除国家/地区对象的本地化名称。因此我建立了两个局部视图。第一个包含国家/地区对象的通用编辑功能,第二个部分视图(将在第一个视图中呈现)包含添加/删除本地化名称的逻辑。

第一个部分视图:

@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 在第一次提交后看起来是相等的。

如有任何帮助,我们将不胜感激。
谢谢

I want to add and remove localized names for a country-object via ajax. Therefore i have build two partial views. The first one contains the generel edit funcionality for the country-object and the second partial view (which will be rendered inside the first one) contains the logic to add/remove localized names.

1st partial view:

@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>

2nd partial view:

@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>

The dedicated controller returns the 2nd partial view when a localized name is either added or removed and replaces itself by adding the content into the 'localizedNamesOverview' from the 1st view. So far this works as i expected.

The problem now is this behaviour works just one time. If i have added or removed a name successfully i cannot delete/add a second one. At the moment i can't see where the problem is, because the generated html looks equals after the first submission.

Any help is appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2024-12-15 09:15:04

好吧,我弄清楚问题是什么:

当发出第一个请求并且重新渲染第二个部分视图时,它会丢失来自父级的 ViewData 信息(countryId),这是生成工作链接所需的(删除每个现有本地化名称的链接)。

同样的问题也适用于周围的 ajax-form,它也会丢失 id。通过对代码进行一些重新定位,我可以解决这个问题。我将 @using( Ajax.BeginForm... 代码从第二个部分视图放置到父视图中,以便在 ajax 请求后不会重新生成。

第一个部分视图(更新):

@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>

}

删除-link 问题可以解决,因为我的模型 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"
                      } )

Okay i figured out what the problem was:

When the first request is made and the 2nd partial view is re-rendered it loses the ViewData information (countryId) from the parent which is needed to generate the working links (delete-link for each existing localized-name).

The same problem applies to the sourrounding ajax-form which also loses the id. Through some repositioning of the code i could fix the problem. I placed the @using( Ajax.BeginForm... code from the 2nd partial view to the parent view so it will not be regenerated after the ajax-request.

1st partial view (updated):

@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>

}

The delete-link problem could be solved because my model LocalizedNameViewModel also contains the dedicated countryId and so i take it from there.

2nd partial view (updated):

@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"
                      } )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文