2

我试图在kendo网格中实现删除功能。当我删除一行时,它可以正常工作,但是当我尝试在此之后立即删除另一行时,它会使用当前记录标识和先前删除的记录标识两次将控件发送到删除操作。我知道我需要在Kendo网格中的CURD操作中返回先前操纵的记录ID,即使这样做也行不通。删除Kendo网格中的记录点击控制器两次

@(Html.Kendo().Grid((IEnumerable<SIMS.Models.Student.BatchModel>)Model.lst_batchmodel) 
    .Name("grid") 
    .Scrollable(s => s.Height("auto")) 
    .Columns(columns => 
    { 

     columns.Bound(o => o.BatchID).Visible(false); 

     columns.ForeignKey(o => o.IntakeYear, (System.Collections.IEnumerable)ViewData["Intake"], "IntakeYearID", "IntakeYear") 
       .Title("Intake").EditorTemplateName("ComboForeignKey"); 
     columns.Bound(o => o.IntakeCode).Title("Intake Code"); 
     columns.Bound(o => o.AcadamiYear).Title("Year"); 
     columns.Bound(o => o.AcadamicSemester).Title("Semester"); 
     columns.ForeignKey(p => p.CampusID, (System.Collections.IEnumerable)ViewData["Campus"], "CampusID", "Name") 
       .Title("Campus").EditorTemplateName("ComboForeignKey"); 
     columns.ForeignKey(p => p.FacultyID, (System.Collections.IEnumerable)ViewData["Faculty"], "id", "FacultyName") 
       .Title("Faculty").EditorTemplateName("ComboForeignKey"); 
     columns.Bound(o => o.WeekEnd_Day).Title("Weekend/day"); 
     columns.Bound(o => o.BatchNo).Title("Batch No.") 
      .ClientTemplate("<a href='/student/editBatch?id=#= BatchID#'>Batch #=BatchNo#</a>"); 
     columns.Bound(o => o.BatchCapacity).Title("Capacity"); 

     columns.Bound(o => o.CurrentCount).Title("Current Count"); 

     columns.Command(command => { command.Destroy(); }); 
     }); 
    }) 
    .ToolBar(toolbar => toolbar.Create()) 
    .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("DetailBatch").Window(o => o.Scrollable(true))) 
    .Pageable() 
    .Sortable() 
    .Filterable() 
    .Groupable() 
    .ClientDetailTemplateId("template") 
    .ToolBar(toolbar => 
    { 
     toolbar.Template(@<text> 
<div class="toolbar"> 
    <a href="/Intake/EditingInline_Read?grid-mode=insert" class="k-button k-button-icontext k-grid-add"> 
     <span class="k-icon k-add"></span>Add New Record 
    </a> 
    <a href="#" id="btnRefresh">Refresh</a> 
</div> 

</text>); 
    }) 
    .DataSource(dataSource => dataSource 

     .Ajax() 
     .PageSize(50) 
     .Events(events => events.Error("error_handler")) 
      .Model(model => { model.Id(p => p.BatchID); }) 
     .Create(update => update.Action("EditingInline_StudentBatch_Create", "Student").Data("getfacultyIDs")) 
     .Read(read => read.Action("EditingInline_StudentBatch_Read", "Student")) 
     .Update(update => update.Action("EditingInline_StudentBatch_Update", "Student")) 
     .Destroy(destroy => destroy.Action("EditingInline_StudentBatch_Delete", "Student")) 
    ) 
    .Resizable(resize => resize.Columns(true)) 
) 


Controller: 

public ActionResult EditingInline_StudentBatch_Delete([DataSourceRequest] DataSourceRequest request, BatchModel model) 
     { 
      repBatch = new BatchRepository(); 
      if (repBatch.getBatchByID(model.BatchID)) 
      { 
       repBatch.DeleteBatch(model.BatchID); 
      } 

      return Json(new[] { model.BatchID }.ToDataSourceResult(request, ModelState)); 

     } 

Repository: 

public void DeleteBatch(int BatchID) 
     { 
      using (context = new SIMSDBAPPEntities()) 
      { 
       try 
       { 
        using (TransactionScope scope = new TransactionScope()) 
        { 
         var batch_users = from tb in context.tblStudent_Batch 
              where tb.BatchID == BatchID 
              select tb; 

         var batch = (from tbb in context.tblBatches 
            where tbb.BatchID == BatchID 
            select tbb).SingleOrDefault(); 

         if (batch_users != null) 
         { 
          foreach (var item in batch_users) 
          { 

           var student = (from tbstu in context.tblStudents 
               where tbstu.StudentID == item.StudentID 
               select tbstu).SingleOrDefault(); 

           if (item.BatchID == student.LatestBatchID) 
           { 
            student.LatestBatchID = null; 
           } 

           context.tblStudent_Batch.Remove(item); 
          } 


          var batch_institute = from tbins in context.tblBatch_Institute 
                where tbins.BatchID == BatchID 
                select tbins; 

          foreach (var Instituteitem in batch_institute) 
          { 
           context.tblBatch_Institute.Remove(Instituteitem); 
          } 

          context.tblBatches.Remove(batch); 

         } 

         Save(); 
         model.BatchID=batch.BatchID; 
         scope.Complete(); 
        } 

       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 
      } 
     } 
+0

您不需要在删除操作时返回标识。 – Brett 2014-12-19 17:17:05

回答

0

Kendo网格中的删除操作完全基于行选择逻辑。删除的内部逻辑类似于:

var selectedRows = grid.select();
$。每个(selectedRows){//命中控制器}

所有你需要做的是,你需要通过调用grid.refresh(),这将再次命中控制器&重新绑定网格或写入清除行选择一些代码可以在成功删除时清除行选择。