【问题标题】:Kendo-UI MVC Grid Action Delete is routing to Create and Create is getting called multiple timesKendo-UI MVC Grid Action Delete 路由到 Create 并且 Create 被多次调用
【发布时间】:2020-01-06 19:04:16
【问题描述】:

首先,这是我第一次……有史以来……!

当更新网格中的数据(内联)时,Delete 会路由到 Create 函数。当我创建(第一条记录)时,它是正确创建的。当我尝试添加另一条记录时,会添加两条记录:我正在创建的记录和原始记录的副本。它似乎在遍历网格中的数据并每次都添加/复制每条记录。

即使我尝试删除,表中的记录也会再次重复

我已验证该表的 ID(这是一个映射表)包含为 model.Id(c => c.Id),甚至尝试为该 ID 添加一个隐藏字段。

更多背景信息,我有两个使用模板设置的嵌入式下拉列表。加载模板时的数据是因为下拉列表中的数据依赖于当前网格中的记录。我不希望用户为同一个自定义字段创建多条记录。

这是网格,通过局部视图在模式窗口中呈现:

        @(Html.Kendo().Grid<Web.Models.Activity.ActivityCustomFieldModel>
            ()
            .Name("CustomMapping-grid")
            .HtmlAttributes(new { style = "height: 250px;" })
            .Columns(columns =>
            {
                columns.Bound(p => p.Id).Hidden(true);
                columns.Bound(p => p.CustomFieldId)
                    .Title("Custom Field")
                    .HeaderHtmlAttributes(new { style = "font-weight: bold" })
                    .EditorTemplateName("CustomFieldListDD")
                    .ClientTemplate("#:CustomFieldName#");
                columns.Bound(p => p.ComponentTypeId)
                    .Title("Component Type")
                    .HeaderHtmlAttributes(new { style = "font-weight: bold" })
                    .EditorTemplateName("ComponentListDD")
                    .ClientTemplate("#:ComponentTypeName#");
                columns.Bound(p => p.IsRequired)
                    .Title("Required")
                    .HeaderHtmlAttributes(new { style = "font-weight: bold; text-align: center" })
                    .HtmlAttributes(new { style = "text-align: center" })
                    .Width(60)
                    .ClientTemplate(reqdTemplate);
                columns.Bound(p => p.IsActive)
                    .Title("Active")
                    .HeaderHtmlAttributes(new { style = "font-weight: bold; text-align: center" })
                    .HtmlAttributes(new { style = "text-align: center" })
                    .Width(60)
                    .ClientTemplate(statusTemplate);
                columns.Command(command =>
                {
                    command.Edit().Text(" ").UpdateText(" ").CancelText(" ");
                    command.Destroy().Text(" ");
                })
                    .HeaderHtmlAttributes(new { style = "text-align: center; font-weight: bold" })
                    .HtmlAttributes(new { style = "text-align: center" })
                    .Title("Action");
            })
            .Scrollable(scr => scr.Height("auto"))
            .Editable(editable => editable.Mode(GridEditMode.InLine))
            .DataSource(dataSource => dataSource
                .Ajax()
                .Events(dsevents => dsevents.Error("error_handler")
                                        .RequestEnd("ReloadGrid"))
                .Model(model =>
                {
                    model.Id(c => c.Id);
                })
                .Read(read => read.Action("CustomFieldMap_Read", "Activity").Data("getActivityInfo"))
                .Create(create => create.Action("CustomFieldMap_Create", "Activity").Data("getActivityInfo"))
                .Update(update => update.Action("CustomFieldMap_Update", "Activity"))
                .Destroy(destroy => destroy.Action("CustomFieldMap_Destroy", "Activity"))
                )
            .Events(gridevents => gridevents.Save("CustomFieldMap_Save"))
        )

这是将所需数据返回给控制器的 javascript

    function getActivityInfo(e) {
        console.log("get mapping parent data")
        var data = {
            SearchId: $("#DivisionId").val(),
            SearchParentId: $('#ParentId').val()
        };
        return data;
    }

    function CustomFieldMap_Save(e) {
        console.log("update cust and comp Id values.")
        var customFieldId = $("#CustomFieldId_CustomFieldId").data("kendoDropDownList").value();
        var componentTypeId = $("#ComponentTypeId_ComponentTypeId").data("kendoDropDownList").value();
        e.model.set("CustomFieldId", customFieldId);
        e.model.set("ComponentTypeId", componentTypeId);
    }

连同控制器代码

        public IActionResult CustomFieldMap_Read([DataSourceRequest] DataSourceRequest request, DivisionSearchModel model)
        {
            // get the Custom field mappings for the parentId value in the DivisionSearchModel
            var mappings = _activityCustomFieldService.GetCustomFieldMappingsforActivity(model.SearchParentId);

            var gridModel = new DataSourceResult
            {
                Data = mappings.Select(x =>
                {
                    var dataModel = _mapper.Map<ActivityCustomFieldModel>(x);
                    dataModel.CustomFieldName = x.CustomField.Name;
                    return dataModel;
                }),
                Total = mappings.Count()
            };
            return Json(gridModel);
        }

        [HttpPost]
        public IActionResult CustomFieldMap_Create([DataSourceRequest] DataSourceRequest request, ActivityCustomFieldModel model, DivisionSearchModel parent)
        {
            if (ModelState.IsValid)
            {
                model.ActivityId = parent.SearchParentId;
                var newMapping = _mapper.Map<ActivityCustomField>(model);
                _activityCustomFieldRepository.Insert(newMapping);
                model.Id = newMapping.Id;
            }
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));

        }

        [HttpPost]
        public IActionResult CustomFieldMap_Update([DataSourceRequest] DataSourceRequest request, ActivityCustomFieldModel model)
        {
            if (ModelState.IsValid)
            {
                _activityCustomFieldService.Update(_mapper.Map<ActivityCustomField>(model));
            }
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }

        [HttpDelete]
        public IActionResult CustomFieldMap_Destroy([DataSourceRequest] DataSourceRequest request, ActivityCustomFieldModel model)
        {
            _activityCustomFieldRepository.Delete(_mapper.Map<ActivityCustomField>(model));
            return Json(new[] { model }.ToDataSourceResult(request, ModelState));
        }
        #endregion

        #region Utilities
        public IActionResult GetCustomFields([DataSourceRequest] DataSourceRequest request, DivisionSearchModel model)
        {
            var AllCustomFields = _customFieldService.GetAllCustomFields(model.SearchId, true);
            var ActivityCustomFields = _activityService.GetCustomFieldsByActivityId(model.SearchParentId);

            var customFields = AllCustomFields.Except(ActivityCustomFields).Select(c => new { fieldId = c.Id, fieldName = c.Name }).ToList(); ;
            return Json(customFields);
        }

只是为了笑,这是下拉列表模板代码

@(Html.Kendo().DropDownList()
    .Name("CustomFieldId")
    .OptionLabel("-- Custom Field --")
    .DataTextField("fieldName")
    .DataValueField("fieldId")
    .HtmlAttributes(new { style = "width: 100%" })
    .DataSource(dataSource => dataSource
            .Read(read => read.Action("GetCustomFields", "Activity").Data("getActivityInfo"))
            )
)

我已经查看和搜索了几个小时,我相信它会很简单......我只是没有看到它。

【问题讨论】:

  • 更新!!我想我已经弄清楚为什么会发生这种情况,但这只会带来另一个我正在努力解决的问题。当我继续调试代码时,我注意到这个结果集上的 ID 没有被检索到,因此值为 0。这肯定会导致网格按照它的方式运行。我现在必须确定的是为什么 Id 为 0。我正在获取其他结果集的 Id,但该表没有返回 Id。如果我无法确定原因,是否可以提交新问题?

标签: javascript c# asp.net-core-mvc kendo-grid kendo-ui-mvc


【解决方案1】:

HttpDelete 属性更改为HttpPost。所有 Kendo UI 客户端方法都使用 POST。

【讨论】:

  • 谢谢你。实际上,我昨晚在复制代码后改变了它。更改不会影响结果操作。
【解决方案2】:

事实证明,在类规范和映射的深处,这个具有复合键和主键的表有一个映射选项

builder.Ignore(mapping => mapping.Id);

导致 Id 未加载到存储库中!一旦被注释掉,它就会按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多