【发布时间】:2022-11-21 17:39:28
【问题描述】:
我想通过按钮将动态字段添加到我的表中。
由于 for 循环取决于模型的计数,因此当我添加一个新对象时,我需要将另一个对象添加到列表中。
<button type="button" onclick="addBtn()">Add</button>
<table>
<tr>
<th>Page</th>
<th>Price</th>
</tr>
<button type="button" onclick="RazorFunction()">Click</button>
@for (int i = 0; i < Model.Books.Count; i++)
{
tr>
<td>
<label asp-for="@Model.Books[i].Page" class="control-label">Page</label>
<input type="number" asp-for="@Model.Books[i].Page" class="form-control" />
</td>
<td>
<label asp-for="@Model.Books[i].Price" class="control-label">Price</label>
<input type="number" asp-for="@Model.Books[i].Price" class="form-control" />
</td>
</tr>
}
</table>
它不起作用,如下所示。
@section Scripts {
<script>
function addBtn() {
@Model.Books.Add(new BooksDto() { Page = 500, Price = 0 });
}
</script>
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
【问题讨论】:
-
你能分享一些关于你的模型的代码吗?
-
你现在的看法是什么?此外,您能否解释一下“在列表中动态添加项目”?如何向表中添加动态字段?结果视图是什么样的?
标签: c# asp.net asp.net-mvc asp.net-core razor