在Kendo网格中删除记录会使控制器两次命中

时间:2014-11-01 05:26:27

标签: asp.net-mvc asp.net-mvc-4 kendo-ui kendo-grid kendo-asp.net-mvc

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

@(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;
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

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

var selectedRows = grid.select();
$ .each(selectedRows){// hit controller}

您需要做的就是通过调用grid.refresh()来清除行选择,这将再次触及控制器&amp;重新绑定网格或编写一些代码,以便在成功删除时清除行选择。