2017-08-17 65 views
-1

这是我使用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> 
+1

所以你甚至没有看到'confirm'?...我的第二个问题是...你确定你的'id == 0' ...还是属性只是'undefined'?... Final问题......如果没有数据,你甚至还会渲染数据吗?它是QuoteLines属性的一部分吗? –

+0

我会在该id上抛出一个空聚并,'data-id =“@(model.QhoteLineID ??”0“)”'或者更好的是,将它包装在一个if中,以检查id是否存在。 – nurdyguy

+0

它的大写M'data-id =“@ Model.QuoteLineID”(不是'@ model'),但我认为这只是一个错字。你什么意思_row没有data_?你是指使用Ajax添加新行吗? ''Quote'属性是什么'QuoteLineID'? –

回答

1

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

请问您可以发布操作方法签名吗?这将有助于看到。

+0

在没有数据的情况下,data-id为0. 它没有达到javascript代码,我有一个断点 '如果(确认...',它永远不会击中它) 如果我尝试发布行,并且他们从服务器返回一些'是必需的'注释。我可以删除t下摆。在此之前,删除事件不会启动。 即使“data-id”为0,他们也可以工作 –

+0

因此,要获取原始数据集,请通过AJAX将其拉入? –

+0

第一组是 '@foreach(var QuoteLine in Model) { Html.RenderPartial(“_ QuoteLineCollectionItem”,QuoteLine); } 额外的行(没有响应)是Ajax动作 'Ajax.ActionLink(“Create QuoteLine”,“AddQuoteLine”,“Quotes”,new {QuoteID = Model.FirstOrDefault()== null?0: Model.First()。QuoteID},新AjaxOptions(){ 列举HTTPMethod = “获取”, InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = “目标ID”' –