简体   繁体   English

Kendo UI MVC Grid始终尝试保存2条记录

[英]Kendo UI MVC Grid always trying to save 2 records

I have configured this grid to autoSync, so that you can edit it and every time you change a field, it will submit the Update action. 我已将此网格配置为autoSync,以便您可以对其进行编辑,并且每次更改字段时,它将提交Update操作。 For this to work, you have to set the Batch(true) property, which supports editing multiple rows, but that is not how I am using it as the autosync automatically fires the update event, you don't get an opportunity to edit multiple rows before calling update. 为此,您必须设置Batch(true)属性,该属性支持编辑多行,但这不是我使用它的方式,因为自动同步会自动触发更新事件,因此您没有机会编辑多个调用update之前的行。

For some reason, it is always submitting a new record Update. 由于某些原因,它总是提交新的记录更新。 For example, when I click the Add New toolbar button toolbar.Create().Text("Add New Time Task"); 例如,当我单击“添加新工具栏”按钮toolbar.Create().Text("Add New Time Task"); ), you can see in the Chrome dev tools network tab that 2 records are being submitted to my controller...a model[0] and model 1 in a single update call. ),您可以在Chrome开发者工具的“网络”标签中看到2条记录正在提交到我的控制器...在单个更新调用中是模型[0]和模型1

But if I perform an InCell edit, and leave focus of the cell to trigger the update call...2 separate update calls are being made...the first one is an Add New (I can tell because the Id = 0 and all the defaults are as per my grid configuration). 但是,如果我执行InCell编辑并留下单元格焦点以触发更新调用...正在进行2个单独的更新调用...第一个是Add New(我可以知道是因为Id = 0且所有默认值是根据我的网格配置)。

Anyone know why this Add New type call is being triggered? 有人知道为什么触发此“添加新类型”调用吗?

@using Kendo.Mvc.UI;

@Html.Partial("_IndexEditViewHeader")

@(Html.Kendo().Grid<OTIS.AppServ.Shared.ViewModels.TimeTaskViewModel>()
    .Name("TimeTasksGrid")
    .Columns(columns =>
    {
        columns.Bound(l => l.Id).Hidden();
        columns.Bound(l => l.CompanyId).Hidden();
        columns.Bound(l => l.CompanyName);
        columns.Bound(l => l.TaskDescription);
        columns.Bound(l => l.TaskTypeId)
            .EditorTemplateName("TimeTaskTypesDDL")
            .ClientTemplate("#= TaskTypeDescription #");
        columns.Bound(l => l.UserSelectable)
            .ClientTemplate("<input type='checkbox' value='#= UserSelectable #' " +
                                "class='chkbx' onclick='setCheckBoxModelValue(this,&quot;TimeTasksGrid&quot;,&quot;UserSelectable&quot;)'" +
                                "# if (UserSelectable) { #" +
                                    "checked='checked'" +
                                "# } #" +
                            " />"
                        );
        columns.Bound(l => l.HoursMultiplier);
        columns.Command(command => command.Destroy()).Width(110);
    })
    .Editable(editing => editing.Mode(GridEditMode.InCell))
    // Add "Create" command
    .ToolBar(toolbar =>
    {
        toolbar.Create().Text("Add New Time Task");
        //toolbar.Save();
    })
    .DataSource(dataSource => dataSource
        .Ajax() // Specify that the data source is of ajax type
        .ServerOperation(false) // paging, sorting, filtering and grouping will be applied client-side
        .Model(model =>
            {
                model.Id(c => c.Id);
                model.Field(c => c.CompanyId).DefaultValue(ViewBag.CompanyId);
                model.Field(c => c.TaskDescription).DefaultValue("Task Description");
                model.Field(c => c.TaskTypeId).DefaultValue(20);
                model.Field(c => c.TaskTypeDescription).DefaultValue("Billable");
                model.Field(c => c.HoursMultiplier).DefaultValue(1);
            }
        )
        .Sort(sort =>
        {
            sort.Add(c => c.CompanyName);
            sort.Add(c => c.TaskDescription);
        })
        // Specify a handler for the error event
        .Events(events => events.Error("KendoGridErrors"))
        .Batch(true)
        // CRUD configuration -->
        .Create(create => create.Action("Grid_SaveOrUpdate", "ManageTimeTasks", new { area = "Shared" }))
        .Read(read => read.Action("Grid_Read", "ManageTimeTasks", new { area = "Shared" })// Specify the action method and controller name
        )
        .Update(update => update.Action("Grid_SaveOrUpdate", "ManageTimeTasks", new { area = "Shared" }))
        .Destroy(destroy => destroy.Action("Grid_Delete", "ManageTimeTasks", new { area = "Shared" }))
            // <-- CRUD configuration      
    )
    .Groupable()
    .Sortable()
    .Filterable()
)

<script>
    $(function () {
        $('#TimeTasksGrid').data().kendoGrid.dataSource.options.autoSync = true;
    });
</script>

Picture of double call to Update upon editing a single cell: 编辑单个单元格时两次调用Update的图片: 在此处输入图片说明

Well that was a fun 6 hours figuring this out. 好了,花了6个小时才弄清楚了。 It turns out that on our test database, someone turned off the Identity default specification on the Id primary key column. 事实证明,在我们的测试数据库中,有人关闭了ID主键列上的Identity默认规范。 Then during testing, a test record with an Id = 0 was saved, so that was being into the grid every time, then when you'd save or update another row, that row with the Id = 0 was also in that batch trying to be saved. 然后在测试过程中,保存了一个Id = 0的测试记录,因此每次都将其记录到网格中,然后当您保存或更新另一行时,该ID = 0的行也在该批处理中被保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM