How can I refresh the grid after I edit the Kendo UI grid?

后端 未结 11 1756
忘掉有多难
忘掉有多难 2021-02-01 15:09

I edit the grid using editable: \"popup\" as shown on Telerik\'s demo page. After I edit the grid, I want the grid to refresh. Does the grid have any event that is

11条回答
  •  情歌与酒
    2021-02-01 15:51

    I agree this is very old question, but unfortunately I was also a victim of this error few days back. My scenario was

    1. First time i was able to insert new record in database using popup method
    2. If I add another record in same grid without refresh, grid is sending both the records to server, and unfortunately leads in duplicate value error.

    Finally I think I got a solution for my problem, I was not setting primary key value to my record when I insert it into database and returning that object.

    Below is my kendo grid code

    @(Html.Kendo().Grid()
            .Name("Grid")
            .Columns(columns =>
            {
                columns.Bound(p => p.abcd);
                columns.Bound(p => p.abcd_id).Hidden();
                columns.Bound(p => p.abcd12_id).Hidden();
                columns.Command(command =>
                {
                    command.Edit();
                    command.Destroy();
                });
            })
            .ToolBar(toolbar =>
            {
                if (ViewBag.IsAddAllowed)
                {
                    toolbar.Create().Text("Add new");
                }
    
                //  toolbar.Save().SaveText("Save Changes");
    
            })
            .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ABCD")
            )
            .Pageable()
            .Navigatable()
            .Sortable()
            .Scrollable()
            .DataSource(dataSource => dataSource
                .Ajax()                
                .PageSize(20)
                .ServerOperation(false)
                .Events(events =>
                {
                    events.Error("error_handler");
                    events.RequestEnd("request_end");
                }
    )
            .Model(model =>
            {
                model.Id(p => p.primarykey_id);
                model.Field(p => p.abcd);
            })
            .Create("ABCD_Create", "TeamMember", new { id = Model.abcd_id})
            .Read("ABCD_Read", "TeamMember", new { id = Model.abcd_id })
            .Update("ABCD_Update", "TeamMember", new { id = Model.abcd_id })
            .Destroy("TeamMember_Destroy", "TeamMember", new { id = Model.hackathon_id })
        )
    )
    

    Below is error handling code

      function error_handler(e) {
            if (e.errors) {
                var message = "Errors:\n";
                $.each(e.errors, function (key, value) {
                    if ('errors' in value) {
                        $.each(value.errors, function () {
                            message += this + "\n";
                        });
                    }
                });
                $(".errorMessage").text(message);
                alert(message);
            }
        }
    
    
    
        function request_end(e) {
            switch (e.type) {
                case "create":
                    if (e.Errors == undefined && e.response.Total > 0) {
                        //  alert("Saved Successfully");
                        $(".errorMessage").text("Saved Successfully");
                    }
                    break;
    
                case "update":
                    if (e.Errors == undefined && e.response.Total > 0) {
                        // alert("Updated Successfully");
                        $(".errorMessage").text("Updated Successfully");
                    }
                    break;
    
                case "destroy":
                    if (e.Errors == undefined && e.response.Total > 0) {
                        // alert("Deleted Successfully");
                        $(".errorMessage").text("Deleted Successfully");
                    }
                    break;
    
                default:
                    $(".errorMessage").text("");
                    break;
            }
    
            return true;
        }
    

    Server Code

     [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ABCD_Create([DataSourceRequest] DataSourceRequest request, MyViewModel my, short abcd_id)
    {
        if (my != null && ModelState.IsValid)
        {
    
            MY tm = Mapper.Map(my);
            tm.abcd_id = abcd_id;
    
            try
            {
                repo.Create(tm);
                my.primarykey_id = tm.primarykey_id;   ///This is most important                           
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Duplicate Value Found", string.Format("error: {0} already exists", my.abcd));
            }
        }
        else
        {
            ModelState.AddModelError("Error", "Not valid please send data again");
        }
    
        return Json(new[] { my }.ToDataSourceResult(request, ModelState));
    }
    

    Hope this might help someone

提交回复
热议问题