gpt4 book ai didi

javascript - 如何使用 JavaScript 插入新的表单行并将其包含在传递给 Controller ​​的内容中?

转载 作者:行者123 更新时间:2023-12-03 02:09:43 25 4
gpt4 key购买 nike

上下文

ASP.NET Core 2.0 MVC/EF Core/C#

问题

我有一个 View ,用于创建新的父行和关联的子行,后者位于 html 表中。最初显示一个空白子行。这一切都被提交给创建 Controller 操作来处理数据库添加,并且工作正常。编码如下:

@using DBWTools.ViewModels
@model IssueViewModel

@section css {
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
}

<h2>New Issue</h2>

<hr />

<form asp-controller="Home" asp-action="CreateIssue" method="post" id="CreateIssueForm">
<div asp-validation-summary="All" class="text-danger"></div>

@* Parent row coding goes here. Removed for brevity *@

<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="col-md-1">Date</th>
<th class="col-md-10">Comment</th>
<th class="col-md-1">
<button type="button" onclick="appendIssueComment()" class="btn btn-primary"
data-toggle="tooltip" title="Add another comment">
<span class="glyphicon glyphicon-plus" />
</button>
</th>
</tr>
</thead>
<tbody class="commentBody">
@for (int i = 0; i < Model.Comments.Count; i++)
{
<tr class="commentRow">
<td>
<input asp-for="@Model.Comments[i].DateCreated" class="form-control" type="date" asp-format="{0:yy-MM-dd}" />
</td>
<td>
<textarea asp-for="@Model.Comments[i].Narrative" class="form-control"></textarea>
</td>
<td style="display:none"><input asp-for="@Model.Comments[i].ID" type="hidden" /></td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="form-horizontal">
<div class="col-md-1">
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
</div>
</form>

@section Scripts {
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

@await Html.PartialAsync("_ValidationScriptsPartial")
<script src="~/js/AppendIssueComment.js" type="text/javascript"></script>
}

该表包含一个带有 onClick 操作的按钮,该操作运行 JavaScript 函数以向表中添加额外的行。其功能是:

function appendIssueComment() {

$(".commentBody").append("<tr>" +
"<td><input class='form-control' type='date' data-val='true' data-val-required='The Date created field is required.' id='Comments_0__DateCreated' name='Comments[0].DateCreated' value=''/></td >" +
"<td><textarea class='form-control' data-val='true' data-val-required = 'The Narrative field is required.' id='Comments_0__Narrative' name='Comments[0].Narrative'></textarea></td >" +
"<td style = 'display:none'><input type='hidden' data-val='true' data-val-required='The ID field is required.' id='Comments_0__ID' name='Comments[0].ID' value='0'/></td > " +
"</tr >");
}

这成功添加了新的空白行,并且可以将数据键入其中。但是,当提交表单时,新行不会传递到 Controller 。

问题

如何向 HTML 表添加新行并将它们成功传递到 Controller ?

编辑

根据@tchelidze 解决方案更新了函数

function appendIssueComment() {

var lastRow = $("[id$='__ID']").length.toString();

$(".commentBody").append("<tr>" +
"<td><input class='form-control' type='date' data-val='true' data-val-required='The Date created field is required.' id='Comments_" + lastRow + "__DateCreated' name='Comments[" + lastRow + "].DateCreated' value=''/></td >" +
"<td><textarea class='form-control' data-val='true' data-val-required = 'The Narrative field is required.' id='Comments_" + lastRow + "__Narrative' name='Comments[" + lastRow + "].Narrative'></textarea></td >" +
"<td style = 'display:none'><input type='hidden' data-val='true' data-val-required='The ID field is required.' id='Comments_" + lastRow + "__ID' name='Comments[" + lastRow + "].ID' value='0'/></td > " +
"</tr >");
}

最佳答案

新行始终绑定(bind)到 Comments[0] 元素,您必须增加每行的索引。

关于javascript - 如何使用 JavaScript 插入新的表单行并将其包含在传递给 Controller ​​的内容中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49633237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com