这是我使用BeginCollectionItem的部分视图中的代码。asp.net mvc Jquery删除表中的空行
<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>
所以你甚至没有看到'confirm'?...我的第二个问题是...你确定你的'id == 0' ...还是属性只是'undefined'?... Final问题......如果没有数据,你甚至还会渲染数据吗?它是QuoteLines属性的一部分吗? –
我会在该id上抛出一个空聚并,'data-id =“@(model.QhoteLineID ??”0“)”'或者更好的是,将它包装在一个if中,以检查id是否存在。 – nurdyguy
它的大写M'data-id =“@ Model.QuoteLineID”(不是'@ model'),但我认为这只是一个错字。你什么意思_row没有data_?你是指使用Ajax添加新行吗? ''Quote'属性是什么'QuoteLineID'? –