My kendo grid is duplicating records

本小妞迷上赌 提交于 2019-12-13 06:57:17

问题


I have a kendo gri in my application.

Here's a picture of it.

And here's the code

@(Html.Kendo().Grid<TekstenViewModel.Tekst>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Template(@<text></text>).ClientTemplate("<input type='checkbox'/>").Width(10).Hidden(!Model.Administrator);

        columns.Bound(product => product.RESOURCE_SET_NAAM).ClientTemplate("#= RESOURCE_SET_NAAM#");

        columns.Bound(product => product.Naam).ClientTemplate("#= Naam#");

        columns.Bound(product => product.Waarde).ClientTemplate("<div id='editorDiv'><div class='input'>#if(Waarde){# #if(Waarde.length>100){# # var myContent =Waarde; #  # var dcontent = myContent.substring(0,100); # <span>#=kendo.toString(dcontent)#</span> #}else{# <span>#=Waarde#</span> #}# #}#</div><div class='editor'>" +
            Html.WebCore().LinkButton(type: ButtonType.Edit, htmlAttributes: new { onclick = "openPopupDemo('#: Waarde #', '#: ID #', 'Waarde')" }));

        columns.Bound(product => product.Opmerking).ClientTemplate("<div id='editorDiv'><div class='input'>#if(Opmerking){# #if(Opmerking.length>100){# # var myContent =Opmerking; #  # var dcontent = myContent.substring(0,100); # <span>#=kendo.toString(dcontent)#</span> #}else{# <span>#=Opmerking#</span> #}# #}#</div><div class='editor'>" +
            Html.WebCore().LinkButton(type: ButtonType.Edit, htmlAttributes: new { onclick = "openPopupDemo('#: Opmerking #', '#: ID #', 'Opmerking')" }));

        columns.Template(@<text></text>).ClientTemplate("<div id='deleteDiv'><div class='delete'><a class=\"delete iconBtn\" onclick=\"deleteResourceItem(#: ID #, '#: Naam #')\"></a></div></div>").Width(10).Hidden(!Model.Administrator);
    })
    .Pageable()
    .Sortable()
    .Filterable()
        .Events(events => events.Edit("onCellEdit").DataBinding("onDataBinding").DataBound("onDataBound"))
    .Groupable()
    .Resizable(a => a.Columns(true))
    .Navigatable()
    .Editable(editable => editable.Mode(GridEditMode.InCell).DisplayDeleteConfirmation(false))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)
        .Events(e => e.Error("error_handler"))
        .Model(model =>
        {
            model.Id(product => product.ID);
            model.Field(product => product.Naam).Editable(Model.Administrator);
            model.Field(product => product.Opmerking).Editable(Model.Administrator);
            model.Field(product => product.Waarde).Editable(!Model.ReadOnly);
            model.Field(product => product.RESOURCE_SET_ID).DefaultValue(Model.SetID);
            model.Field(product => product.Type).DefaultValue(Domain.Agromilieu2.Common.Objects.Entities.Resources.ResourceType.GLOBAL_RESOURCES);
            model.Field(product => product.Taal).DefaultValue(Domain.Agromilieu2.Common.Agromilieu2Constants.Resources.DEFAULT_TAAL_CODE);
        })
        .Create(create => create.Action(MVC.BeheerTeksten.ActionNames.ResourceItems_Create, MVC.BeheerTeksten.Name).Data("onCreateAdditionalData"))
        .Read(read => read.Action(MVC.BeheerTeksten.ActionNames.ResourceItems_Read, MVC.BeheerTeksten.Name, new { setID = Model.SetID }).Data("onReadAdditionalData"))
        .Update(update => update.Action(MVC.BeheerTeksten.ActionNames.ResourceItems_Update, MVC.BeheerTeksten.Name).Data("onAdditionalData"))
        .Destroy(destroy => destroy.Action(MVC.BeheerTeksten.ActionNames.ResourceItems_Delete, MVC.BeheerTeksten.Name))
        )
)

The grid works just fine, except when I create a new record. It calls the method to create, saves the record on the database, etc, no problem.

But when I return to the view, the record I just add appears as a duplicate of the first record inserted in the table.

Yes, I know it sounds strange but it's true. I've tried several times with different values, but I always get the same result.

Every new record is shown as a duplicate of the first record added to the grid. Off course, if I refresh the page, the data is correct.

Here's an example. The grid before adding a new record.

The grid after inserting a new record.

Finally, after saving changes, I get this. If I refresh the page, the data is correct.

And here's my create method.

[AcceptVerbs(HttpVerbs.Post)]
        [Domain.BasisArchitectuur.Framework.MVC.ActionFilters.MenuItem("Teksten")]
        [IsAgromilieuActieAllowed(ActieClaims.TekstenBeheren)]
        public virtual ActionResult ResourceItems_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<TekstenViewModel.Tekst> resourceItems, long setID, string toepassingsCode)
        {
            List<ResourceItemDto> entities = new List<ResourceItemDto>();

            if (ModelState.IsValid && SecurityHelper.IsActieAllowed(ActieClaims.TekstenBeheren))
            {
                try
                {
                    using (IProxy<IResourceService> proxy = _proxyFactory.Create<IResourceService>())
                    {
                        foreach (TekstenViewModel.Tekst tekstenViewModel in resourceItems)
                        {
                            ResourceItemDto resourceItem = new ResourceItemDto
                            {
                                ResourceItem_ID   = tekstenViewModel.ID,
                                ResourceSet_ID    = setID,
                                Naam              = HttpUtility.HtmlDecode(tekstenViewModel.Naam),
                                Opmerking         = HttpUtility.HtmlDecode(tekstenViewModel.Opmerking),
                                Waarde            = HttpUtility.HtmlDecode(tekstenViewModel.Waarde),
                                Type_Code         = tekstenViewModel.Type,
                                Taal_Code         = tekstenViewModel.Taal,
                                ResourceWaarde_ID = tekstenViewModel.WAARDE_ID
                            };

                            entities.Add(resourceItem);
                        }

                        proxy.Client.CheckIfNameExists(entities, toepassingsCode);
                        proxy.Client.AddOrUpdateResourceItem(entities.AsEnumerable());
                    }
                }                

                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            else
            {
                var errMsg = ModelState.Values
                    .Where(x => x.Errors.Count >= 1)
                    .Aggregate("Model State Errors: ", (current, err) => current + err.Errors.Select(x => x.ErrorMessage));

                ModelState.AddModelError(string.Empty, errMsg);
            }

            resourceItems = GetResourceItemsList(new TekstenViewModel.ZoekCriteria { Taal = resourceItems.FirstOrDefault().Taal, ResourceSet_ID = resourceItems.FirstOrDefault().RESOURCE_SET_ID });

            return Json(resourceItems.ToDataSourceResult(request, ModelState));
        }

回答1:


The problem should be that you are not returning a Json with product.ID back from your create action

Create(create => create.Action(MVC.BeheerTeksten.ActionNames.ResourceItems_Create, MVC.BeheerTeksten.Name).Data("onCreateAdditionalData"))

Need to Return , the added item with the model.Id(product => product.ID);

Kendo grid tracks the Items when created by the ID so you need to provide it back to the DataSource



来源:https://stackoverflow.com/questions/25563942/my-kendo-grid-is-duplicating-records

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!