无法更新剑道网格中的数据,总是创建数据

本文关键字:数据 创建 更新 网格 | 更新日期: 2023-09-27 17:57:52

我想在网格中内联数据CRUD。创建方法已成功完成。但当我尝试进行更新时,它总是会创建新的数据。当我尝试进行调试时,它总是转到create方法。

我使用的是KendoUI,C#中的MVC4。

型号

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace admission.Models
{
    public class JenisUjianModel
    {
        public JenisUjianModel() { }
        public int id_jenis_ujian { set; get; }
        public string jenis_ujian { set; get; }
        public DateTime tgl_ujian { set; get; }
        public string created { set; get; }
        public string createdBy { set; get; }
        public string modified { set; get; }
        public string modifiedBy { set; get; }
        public string flag { set; get; }
    }
    public class UjianJenisModel
    {
        pmbEntities entities = new pmbEntities();
        public IEnumerable<JenisUjianModel> Read()
        {
            return entities.Tbl_Jenis_Ujian.Select(jnis => new JenisUjianModel
            {
                id_jenis_ujian = jnis.id_jenis_ujian,
                jenis_ujian = jnis.jenis_ujian,
                tgl_ujian = jnis.Tgl_Ujian.Value
            }).OrderBy(u => u.id_jenis_ujian);
        }
        public void Create(JenisUjianModel jnis)
        {
            var entity = new Tbl_Jenis_Ujian();
            int last = entities.Tbl_Jenis_Ujian.Max(u => u.id_jenis_ujian);
            int coun = entities.Tbl_Jenis_Ujian.Count();
            if (coun == 0)
            {
                entity.id_jenis_ujian = 0;
            }
            else
                entity.id_jenis_ujian = last + 1;
            entity.jenis_ujian = jnis.jenis_ujian;
            entity.Tgl_Ujian = jnis.tgl_ujian;
            entity.created = DateTime.Now;
            entity.flag = "Y";
            entities.Tbl_Jenis_Ujian.Add(entity);
            entities.SaveChanges();
        }
        public void Update(JenisUjianModel jnis)
        {
            var entity = new Tbl_Jenis_Ujian();
            entity.id_jenis_ujian = jnis.id_jenis_ujian;
            entity.jenis_ujian = jnis.jenis_ujian;
            entity.Tgl_Ujian = jnis.tgl_ujian;
            entity.modified = DateTime.Now;
            entity.modifiedBy = jnis.modifiedBy;
            entity.created = DateTime.Parse(jnis.created);
            entity.createdBy = jnis.createdBy;
            entity.flag = jnis.flag;
            entities.Tbl_Jenis_Ujian.Attach(entity);
            entities.Entry(entity).State = EntityState.Modified;
            entities.SaveChanges();
        }
        public void Destroy(JenisUjianModel jnis)
        {
            var entity = new Tbl_Jenis_Ujian();
            entity.id_jenis_ujian = jnis.id_jenis_ujian;
            entity.jenis_ujian = jnis.jenis_ujian;
            entity.Tgl_Ujian = jnis.tgl_ujian;
            entity.modified = DateTime.Parse(jnis.modified);
            entity.modifiedBy = jnis.modifiedBy;
            entity.created = DateTime.Parse(jnis.created);
            entity.createdBy = jnis.createdBy;
            entity.flag = "N";
            entities.Tbl_Jenis_Ujian.Attach(entity);
            entities.Entry(entity).State = EntityState.Modified;
            entities.SaveChanges();
        }
    }
}

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using admission.Models;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System.Data.Entity;
namespace admission.Controllers
{
    public class UjianController : Controller
    {
        //
        // GET: /Ujian/
        UjianJenisModel uj = new UjianJenisModel();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult JenisUjian() { return View(); }
        
        public ActionResult jenisUjian_read([DataSourceRequest] DataSourceRequest request)
        {
            return Json(uj.Read().ToDataSourceResult(request));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult jenisUjian_Create([DataSourceRequest] DataSourceRequest request, JenisUjianModel jnis)
        {
            if (ModelState.IsValid)
            {
                uj.Create(jnis);
            }
            return Json(new[] { jnis }.ToDataSourceResult(request, ModelState));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult jenisUjian_Update([DataSourceRequest] DataSourceRequest request, JenisUjianModel jnis)
        {
            if (ModelState.IsValid)
            {
                uj.Update(jnis);
            }
            return Json(new[] { jnis }.ToDataSourceResult(request, ModelState));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult jenisUjian_Delete([DataSourceRequest] DataSourceRequest request, JenisUjianModel jnis)
        {
            if (jnis != null)
            {
                uj.Destroy(jnis);
            }
            return Json(new[] { jnis }.ToDataSourceResult(request, ModelState));
        }
    }
}

查看

@using Kendo.Mvc.Extensions
@{
    ViewBag.Title = "JenisUjian";
    Layout = "~/Views/Shared/_adminLayout.cshtml";
}
<div class="tables">
    <div class="table-responsive bs-example widget-shadow">
        <h4>Data Propinsi:</h4>
            @(Html.Kendo().Grid<admission.Models.JenisUjianModel>()
    .Name("gridss")
    .Columns(columns =>
    {
        columns.Bound(p => p.id_jenis_ujian).Hidden();
        columns.Bound(p => p.jenis_ujian).Title("Jenis Ujian");
        columns.Bound(p => p.tgl_ujian).Title("Tgl Ujian").Format("{0: dd MMM yyyy - HH:mm WIB}");
        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(250);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Sortable()
    .Filterable()
    .HtmlAttributes(new { style = "height:570px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .Events(events => 
        { 
            events.Error("error_handler");
            events.RequestEnd("force_update");
        })
        .Model(model => model.Id(p => p.id_jenis_ujian))
        .Create(update1 => update1.Action("jenisUjian_Create", "Ujian"))
        .Update(update2=> update2.Action("jenisUjian_Update", "Ujian"))
        .Read(read => read.Action("jenisUjian_read", "Ujian"))
        .Destroy(update3 => update3.Action("jenisUjian_Delete", "Ujian"))
    )
)
    </div>
</div>
<script type="text/javascript">
    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";
                    });
                }
            });        
            alert(message);
        }
    }
</script>
<script type="text/javascript">
    function force_update(e) {
        if (e.type === "create") {
            location.reload();
        }
    }
</script>

无法更新剑道网格中的数据,总是创建数据

如果您发现创建总是发生,这将是因为生成id时网格的default特性。

当将项目保存回您的系统时,我假设它会在某个地方自动递增主键。这个new id需要分配给返回给网格的模型,这样它就知道如果对此项进行进一步更改,将生成更新操作。

所以在代码部分:

 uj.Create(jnis);

更改此项,以便将id分配回模型。也许是这样的:

jnis.id_jenis_ujian = uj.Create(jnis); 

jnis = uj.Create(jnis);

然后更改Create签名以返回int或模型,而不是将其作为void。

 public int Create(JenisUjianModel jnis)
 {
     ....Code removed for brevity
     int returnId = lastId + 1; 
     return returnId; 
 }

public JenisUjianModel Create(JenisUjianModel jnis)
     {
         ....Code removed for brevity
         jnis.id_jenis_ujian = lastId + 1; 
         return jnis; 
     }

希望这能帮到你。

任何问题都可以问。

使用下面的链接下载并安装Fiddler

https://www.telerik.com/download/fiddler

正如@David Shorthose所提到的,身份证一定是在途中的某个地方丢失的。

使用Fiddler检查您发送到行动的数据,或使用以下代码行在Chrome开发工具(F12)中调试您的网格数据:

debugger;
$("#gridss").data("kendoGrid").dataSource.data();

如果您不能自己解决问题,请报告您收集的数据,使我们能够帮助您更有洞察力。