无法更新Kendo网格中的数据,始终创建数据

时间:2016-07-20 07:59:18

标签: c# asp.net-mvc-4 kendo-grid telerik-mvc

我想在网格中内联数据CRUD。 Create方法成功完成。但是当我尝试进行更新时,它总是会创建新数据。当我尝试做调试。它总是转到create方法。请有人帮帮我。我在C#中使用了KendoUI,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>

2 个答案:

答案 0 :(得分:0)

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

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

所以在代码部分:

 uj.Create(jnis);

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

jnis.id_jenis_ujian = uj.Create(jnis); 

jnis = uj.Create(jnis);

然后更改您创建签名以返回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; 
     }

希望这可以帮到你。

任何问题都可以随意提问。

答案 1 :(得分:0)

使用以下链接下载并安装Fiddler

  

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

正如@David Shorthose所提到的那样,ID必须在某个地方错过。

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

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

如果您无法自行解决,请报告您收集的数据,以便我们能够帮助您提高洞察力。