以编程方式 return 具有正确索引的部分视图

Programmatically return Partial View with correct index

我有一个视图正在使用动态行在 People 列表中呈现 Person 对象。我在 HTML:

中这样渲染它
<partial name="_People" for="Model.People[i]" />

其中 i 是 for 循环中的索引。

然后它会使用正确的输入名称呈现部分视图,以便我可以绑定回它。像这样:

<input name="People[0].Name" ...
<input name="People[0].PhoneNumber" ...

我想回调我的剃刀模型以编程方式呈现新行并使其 return 成为具有正确索引的行。所以说 table 中已经有 7 行,我点击了一个按钮,它 return 是一个部分视图,其中我的行输入名称中的正确索引为 8。然后我可以使用 javascript 将此行附加到 table。所以只是想知道是否有一个程序等同于

<partial name="_People" for="Model.People[8]" />

我明白了。您可以在视图数据模板信息中设置 HTML 字段前缀。

像这样:

int index = 8;
ViewData.TemplateInfo.HtmlFieldPrefix = string.format("People[{0}], index)";