Kendo Grid Update按钮调用创建功能

时间:2013-09-11 14:01:29

标签: asp.net-mvc-4 kendo-grid

当我点击更新按钮,如果我没有添加任何新列,工作正确但是在我添加新列之后,更新按钮有时会调用更新功能,有时会调用create function。这就是我的问题

查看

@(Html.Kendo().Grid(Model)
    .Name("dersler")
    .Columns(columns =>
    {
        columns.Bound(p => p.Tanim).Width(200);
        columns.Bound(p => p.Aciklama).Width(100);
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp)
                            .Window(conf => conf.Title("Yeni Ders"))
                            .TemplateName("DersTemplate"))
    .Pageable()
    .Sortable()
    .Scrollable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
        .Model(model => model.Id(p => p.Id))
        .Create(create => create.Action("DersOlustur", "Tanim"))
        .Update(update => update.Action("DersGuncelle", "Tanim"))
        .Destroy(destroy => destroy.Action("DersSil", "Tanim"))
    )
)

控制器

#region Ders

        public ActionResult Dersler()
        {
            return View(Helper.Islemci.DersleriVer());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public void DersOlustur([DataSourceRequest] DataSourceRequest request, Ders model)
        {
            if (model != null && ModelState.IsValid)
            {
               Helper.Islemci.DersTanimla(model);
            }

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DersGuncelle([DataSourceRequest] DataSourceRequest request, Ders ders)
        {
            if (ders != null && ModelState.IsValid)
            {
                var target = Helper.Islemci.DersAra(ders.Id);
                if (target != null)
                {
                    target.Tanim = ders.Tanim;
                    target.Aciklama = ders.Aciklama;
                    ders = Helper.Islemci.DersGuncelle(target);
                }
            }

            return Json(new[] { ders }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DersSil([DataSourceRequest] DataSourceRequest request, Ders ders)
        {
            if (ders != null)
            {
                Helper.Islemci.DersSil(ders);
            }

            return Json(new[] { ders }.ToDataSourceResult(request, ModelState));
        }
        #endregion

和我的助手*

#region Ders
        public List<Kurslar.Models.Ders> DersleriVer()
        {

            string sql = "SELECT * FROM KURS.DERSLER";

            var dersler = DataManager.ReadIntoList<Kurslar.Models.Ders>(sql, _dbnumber);

            return dersler;
        }
        public Kurslar.Models.Ders DersAra(int ID)
        {

            string sql = "SELECT * FROM KURS.DERSLER WHERE ID={0}";
            sql = string.Format(sql, ID);

            var ders = DataManager.ReadIntoList<Kurslar.Models.Ders>(sql, _dbnumber);

            return ders.FirstOrDefault();
        }
        public Ders DersGuncelle(Ders model)
        {
            model.DurumKodu = "A";
            model.IslemNo = model.IslemNo + 1;

            IDatabase db = DataManager.GetDatabase(_dbnumber);
            using (DbConnection conn = db.CreateConnection())
            {
                conn.Open();
                using (DbTransaction trans = conn.BeginTransaction())
                {
                    model.Update(trans);
                    trans.Commit();
                }
            }
            return model;
        }

        public void DersTanimla(Ders ders )
        {
            ders.DurumKodu= "A";
            ders.IslemNo= 1;

            IDatabase db = DataManager.GetDatabase(_dbnumber);
            using (DbConnection conn = db.CreateConnection())
            {
                conn.Open();
                using (DbTransaction trans = conn.BeginTransaction())
                {
                    ders.Id = (short)db.GetNextIdFor(ders.GetType(), trans);
                    ders.Insert(trans);
                    trans.Commit();
                }
            }
        }
        public Ders DersSil(Ders ders)
        {

            IDatabase db = DataManager.GetDatabase(_dbnumber);
            using (DbConnection conn = db.CreateConnection())
            {
                conn.Open();
                using (DbTransaction trans = conn.BeginTransaction())
                {
                    ders.Delete(trans);
                    trans.Commit();
                }
            }
            return ders;
        }
        #endregion

2 个答案:

答案 0 :(得分:1)

Dersoluolut功能必须像插入后刷新网格一样

 [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult DersOlustur([DataSourceRequest] DataSourceRequest request, Ders model)
    {
        if (model != null && ModelState.IsValid)
        {
           Helper.Islemci.DersTanimla(model);
        }
        return Json(new[] { model }.ToDataSourceResult(request, ModelState));
    }

答案 1 :(得分:0)

将此添加到您的列中它将起作用:

columns.Bound(p => p.Id); // ID is the ID of the view model of course; i.e.,  your ID (primary key col.)

当您单击更新时,它不知道它应该为更新采用哪个ID,因此 - 将整个视图模型带入更新操作方法,即当然你所有的行......

您不必显示id列,但它必须在那里。

所以上面可以这样做:

columns.Bound(p => p.Id).Visible(false);

这对我有用。