All grid delete button not working

2 posts, 0 answers
  1. Jalen
    Jalen avatar
    9 posts
    Member since:
    Oct 2016

    Posted 13 Nov 2016 Link to this post

    Recently I got one important problem that all my kendo grid delete button are not working.

    But create and update button work fine.

    I didn't know when this happened, because my recent developing function not contain delete button.

    Here's one of my code:

     

            @(Html.Kendo().Grid<Template.Areas.BasicInfo.Models.COMM_ItemList>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.ForeignKey(p => p.ListName, (System.Collections.IEnumerable)ViewData["ListName"], "ItemValue", "ItemName").Width(100);
            //columns.Bound(p => p.ItemIndex).Width(120);
            columns.Bound(p => p.ItemName).Width(120);
            columns.Bound(p => p.ItemValue).Width(120);
            columns.Bound(p => p.ItemMemo).Width(120);
            columns.Command(command =>
            {
                command.Edit().Text("修改");
                command.Destroy().Text("刪除");
            }).Width(160);
        })
        .ToolBar(toolbar =>
        {
            toolbar.Create().Text("新增");
            toolbar.Excel().Text("匯出Excel");
            //toolbar.Save();
        })
        .Editable(editable => editable.Mode(GridEditMode.PopUp))
        .Pageable(pager => pager.Input(true).Numeric(true).Info(true).PreviousNext(true).Refresh(true).PageSizes(true))
        .Sortable(sortable => sortable.AllowUnsort(true).SortMode(GridSortMode.MultipleColumn))
        .Scrollable()
        .Reorderable(reorder => reorder.Columns(true))
        .Resizable(resize => resize.Columns(true))
        .Excel(excel => excel.FileName(ViewBag.Title + ".xlsx"))
        .HtmlAttributes(new { style = "height:400px;width:100%" })
        .Events(events => events.Edit("edit").Save("onSave"))
        .DataSource(dataSource => dataSource
            .Ajax()
            //.Batch(false)
            //.ServerOperation(false)
            .PageSize(10)
            .Events(events => events.Error("error_handler"))
            .Model(model =>
            {
                model.Id(p => p.ListName);
                //model.Id(p => p.ItemIndex);
                //model.Field(p => p.CreateUserId).DefaultValue(User.Identity.Name); //設定CreateUserId的預設值
            })
            .Create(Create => Create.Action("COMM_ItemList_Create", "ItemListSetting"))
            .Read(Read => Read.Action("COMM_ItemList_Read", "ItemListSetting"))
            .Update(Update => Update.Action("COMM_ItemList_Update", "ItemListSetting"))
            .Destroy(Destroy => Destroy.Action("COMM_ItemList_Destroy", "ItemListSetting"))
            )
        )

     

     

     

    namespace Template.Areas.BasicInfo.Controllers
    {
        public class ItemListSettingController : BaseApiController
        {
            // GET: BasicInfo/ItemListSetting
            public ActionResult Index()
            {
                try
                {
                    ItemList();
                    return View();
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "ItemListSettingController");
                    return new JavaScriptResult { Script = "alert('" + ex.ToString() + "');" };
                }
            }


            public ActionResult COMM_ItemList_Read([DataSourceRequest] DataSourceRequest request)
            {
                try
                {
                    //var LimsMntList = db.COMM_ItemList.Where(o => o.ListName == "LimsMntList").ToList();
                    return Json(db.COMM_ItemList.Where(o => o.ListName != "LimsMntList").ToDataSourceResult(request));
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "ItemListSettingController");
                    return new JavaScriptResult { Script = "alert('" + ex.ToString() + "');" };
                }
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult COMM_ItemList_Update([DataSourceRequest] DataSourceRequest request, COMM_ItemList o)
            {
                try
                {
                    if (o != null && ModelState.IsValid)
                    {
                        db.Entry(o).State = System.Data.Entity.EntityState.Modified;
                        //o.LastUpdate = User.Identity.Name;
                        //o.UpdateTime = DateTime.Now;
                        db.SaveChanges();
                    }
                    return Json(new[] { o }.ToDataSourceResult(request, ModelState));
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "ItemListSettingController");
                    return new JavaScriptResult { Script = "alert('" + ex.ToString() + "');" };
                }
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult COMM_ItemList_Create([DataSourceRequest] DataSourceRequest request, COMM_ItemList o)
            {
                try
                {
                    if (o != null && ModelState.IsValid)
                    {
                        var q = db.COMM_ItemList.Where(m => m.ItemName == o.ItemName || m.ItemValue == o.ItemValue).ToList();
                        if (q.Count > 0)
                        {
                            return new JavaScriptResult { Script = "alert('!!');$('#grid').data('kendoGrid').dataSource.read();" };
                            //RedirectToAction("UserIndex", new JavaScriptResult { Script = "alert('!');" });
                        }
                        else
                        {
                            var num = db.COMM_ItemList.Where(i => i.ListName == o.ListName).Max(i => i.ItemIndex).ToString();
                            o.ItemIndex = Convert.ToInt16(Convert.ToInt16(num) + 1);
                            db.COMM_ItemList.Add(o);
                            db.SaveChanges();
                        }
                    }
                    return Json(new[] { o }.ToDataSourceResult(request, ModelState));
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "ItemListSettingController");
                    return new JavaScriptResult { Script = "alert('" + ex.ToString() + "');" };
                }
            }

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult COMM_ItemList_Destroy([DataSourceRequest] DataSourceRequest request, COMM_ItemList o)
            {
                if (o != null)
                {
                    try
                    {
                        COMM_ItemList Item = db.COMM_ItemList.Find(o.ListName, o.ItemIndex);
                        db.COMM_ItemList.Remove(Item);
                        db.SaveChanges();                  
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "ItemListSettingController");
                        return new JavaScriptResult { Script = "alert('" + ex.ToString() + "');" };
                    }
                }
                return Json(new[] { o }.ToDataSourceResult(request, ModelState));
            }
        }
    }

     

     

        public class COMM_ItemList
        {
            [Key]
            [Required]
            [Column(Order = 1)]
            [UIHint("ItemList_ListName")]
            public string ListName { get; set; }
            [Key]
            [Required]
            [Column(Order = 2)]
            public short ItemIndex { get; set; }
            [Required]
            public string ItemName { get; set; }
            [Required]
            public string ItemValue { get; set; }
            //public string ValueType { get; set; }
            public string ItemMemo { get; set; }
        }



  2. Jalen
    Jalen avatar
    9 posts
    Member since:
    Oct 2016

    Posted 13 Nov 2016 Link to this post

    I found the problem.

    I created a javascript function called confirm() in _Layout page last week, so every page would include this function.

    But kendo grid have its own confirm() function in javascript which name is duplicated.

    After I changed my function name,  delete button of kendo grid worked fine again. Thank god!!

    Therefore, DO NOT name a javascript function name confirm() in your page if you need use kendo grid delete button.

Back to Top