ASP.Net MVC 5:Try 将模型值添加到 for 循环中的 Ajax Actionlink 目标 id / div 元素 -

ASP.Net MVC 5:Try to add a model value to Ajax Actionlink target id / div element within a for loop -

我正在开发一个项目(对这个主题来说很新),用户可以在其中为一个公司添加多个地址。 现在我想在我这边添加一个局部视图(通过单击一个操作 link),以便可以为每个地址输入多个 phone 数字。

空子部分(对于 phone 和..)应直接放置在单击操作 link 的地址之后(固有的 div 元素放置在那里)

例如,我有两个 addesses 公司 xyz 我呈现两个动作 links 和每个待处理的 div 元素。

如何让我的 ajax 操作 link 只更新 for 循环中对应的 div?我的意思是,如果第一个 link 被点击,部分视图应该在第一个 div 元素位置呈现,依此类推。我有一个 addressId 可用于将其添加到 link 和 div.

的 ID

部分由控制器正确返回(使用 Fiddler 检查),但不会在页面上呈现。 如果我在 ActionLink 的 "UpdateTargetId" 和占位符 div 中删除对我的 "AddressId" 的引用 - 一切正常。

如果我检查网站源代码,我在 targetId 和 div id 中有相同的 "names" (ComRow+1) - 请参阅:

... data-ajax="true" data-ajax-mode="after" data-ajax-update="#ComRow+ 1" href="/companyDetails/GetCommunicationEditor/1">为此地址添加新的 Phone 号码/电子邮件...

和:

        </div>
        <div id="ComRow+1">

            </div>

我发现了这个:

dynamic update target id in Ajax.ActionLink

但它对我不起作用(也许我在 for 循环中?)

还有这个: How to add a Model Primary Key which is INT, to an ActionLink LinkText

但是“.ToString()”没有解决问题

我做错了什么?非常感谢

这是生成循环的代码:

<div>

    @for (int i = 0; i < Model.EditorAddresses.Count(); i++)
    {

        @Html.EditorFor(m => m.EditorAddresses[i])

        //Start - render link to add template for the new address
        <div>

            @Ajax.ActionLink("Add a new Phone no./Email for this Address...",
       "GetCommunicationEditor", new { @id = Model.EditorAddresses.ElementAt(i).AddressId },
       new AjaxOptions
       {
           InsertionMode = InsertionMode.InsertAfter,
           UpdateTargetId = "ComRow+" + Model.EditorAddresses.ElementAt(i).AddressId.ToString(),
       });

        </div>
        //End - render link to add template for the new address



        <div id="ComRow+@Model.EditorAddresses.FirstOrDefault().AddressId.ToString()">

            </div>

        //<div id="ComRow+<%=Model.EditorAddresses.ElementAt(i).AddressId%>"></div>

        ...
    }

自己解决了问题!我删除了字符串元素 "ComRow".

我修改了 Ajax 动作链接:

UpdateTargetId = Model.EditorAddresses.ElementAt(i).AddressId.ToString(),

结果是:data-ajax-update="#1"(网站 "view source code")

而且我还将 "div id" 的代码更改为:

id="@Model.EditorAddresses.ElementAt(i).AddressId.ToString()"

结果:

id="1" 如果我​​看网站源码

P.S。 div 元素“...FirstOrDefault()..”的发布代码也有错误。正确的方法是使用 "...ElementAt(i)..".

我认为问题在于,div id 不接受“+”

也许这对某人有帮助