1

when i click update button if i didn't add any new column, work correct but after i add new column, update button sometimes call update function, sometimes call create function. thats my problem

view

@(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"))
    )
)

controller

#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

and my helper *

#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
4

2 回答 2

1

Ders oluştur 函数必须像我在插入后忘记刷新网格

 [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));
    }
于 2013-09-11T14:07:32.663 回答
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);

这对我有用。

于 2014-01-17T16:59:52.457 回答