gpt4 book ai didi

c# - Kendo grid更新数据失败,总是创建数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:27 25 4
gpt4 key购买 nike

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

我在 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();
}
}
}

Controller

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 signature 以返回 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;
}

希望这对您有所帮助。

如有任何问题,请随时提出。

关于c# - Kendo grid更新数据失败,总是创建数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com