asp.net mvc Jquery 从表中删除空行



这是我使用BeginCollectionItem的部分视图中的代码。

<tr>
@using (Html.BeginCollectionItem("QuoteLines"))
{
<td>
@Html.HiddenFor(m => m.QuoteID)
@Html.HiddenFor(m => m.QuoteLineID)
</td>
<td class="visible-lg  col-lg-3">
@Html.TextBoxFor(m => m.Group, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Group, "", new { @class = "text-danger" })
</td>
<td class="col-xs-9 col-sm-9 col-md-8 col-lg-5">
@Html.TextAreaFor(m => m.Description, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</td>
<td class="visible-md visible-lg col-md-2  col-lg-2">
@Html.TextBoxFor(m => m.Quantity, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</td>
<td class="col-xs-3 col-sm-3 col-md-2 col-lg-2">
@Html.TextBoxFor(m => m.Price, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</td>
<td>
<button type="button" class="delete form-control btn-default" data-id="@model.QuoteLineID">Delete</button>
</td>
}

重要的部分是删除按钮,它有一个由javascript引用的类来删除行。但由于某种原因,如果行没有数据,它不会执行代码。

<button type="button" class="delete form-control btn-default" data-id="@model.QuoteLineID">Delete</button>

Javascript代码:

<script>
var url = '@Url.Action("DeleteQuoteLine")'; // assumes its in the same controller
$('.delete').click(function () {

if (confirm('verwijderen?')) {
var id = $(this).data('id');
var row = $(this).closest('tr');
if (id == 0) { // or if(id == 0) depending if your property is nullable
row.remove(); // the item never existed so no need to call the server
return;
}
$.post(url, { ID: id }, function (response) {
if (response) {
row.remove(); // OK, so remove the row
} else {
// Oops - display and error message?
}
});
}
});
</script>

行没有数据是什么意思? 如果行没有数据,因此没有存储在data-id中的数字 ID 值,那么原因可能是帖子期望有一个有效类型的 ID,但它是空的,因此 Action 方法抛出错误(使用错误处理 ASP.NET 会被捕获......如果要记录这些错误,请检查日志(。 若要验证这一点,请使用网络工具查看它是否向服务器发出请求并返回错误响应。

你能发布操作方法签名吗? 这将有助于看到。

最新更新