我正在尝试使用Kendo UI构建ASP.NET App,当我想创建,更新和销毁时,我遇到了问题。我构建它时没有错误。它显示良好,当我尝试添加一些东西时,它应该如此。它仍然显示我刚刚创建的内容。但刷新页面后数据不存在。 数据库没有任何变化,没有数据添加。
这是我的控制器:
public ActionResult _Index([DataSourceRequest]DataSourceRequest request)
{
var u = db.Unit.Select(n => new { n.UnitID,n.KodeUnit, n.NamaUnit, n.JumlahPegawai });
DataSourceResult result = u.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Unit> unit)
{
var results = new List<Unit>();
if (unit != null && ModelState.IsValid)
{
foreach (var u in unit)
{
db.Unit.Add(u);
db.SaveChanges();
}
}
return Json(results.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Unit> unit)
{
if (unit != null && ModelState.IsValid)
{
foreach (var u in unit)
{
db.Entry(u).State = EntityState.Modified;
db.SaveChanges();
}
}
return Json(new[] { unit }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Unit> unit)
{
if (unit.Any())
{
foreach (var u in unit)
{
db.Unit.Remove(u);
db.SaveChanges();
}
}
return Json(unit.ToDataSourceResult(request, ModelState));
}
这是我的模特:
namespace Aplikasi_PKPT.Models
{
public class Unit
{
[Key]
public int UnitID { get; set; }
[Display(Name="Kode Unit")]
[Required(ErrorMessage="Kode Unit Tidak Boleh Kosong")]
public string KodeUnit { get; set; }
[Display(Name = "Nama Unit")]
[Required(ErrorMessage = "Nama Unit Tidak Boleh Kosong")]
[StringLength(60, ErrorMessage = "Nama Unit Tidak Boleh Melebihi 60 Karakter")]
public string NamaUnit { get; set; }
[Display(Name = "Jumlah Pegawai")]
[Required(ErrorMessage = "Jumlah Pegawai Tidak Boleh Kosong")]
public int JumlahPegawai { get; set; }
public virtual ICollection<Pkpt> Pkpt { get; set; }
public virtual ICollection<User> User { get; set; }
public virtual ICollection<Korkel> Korkel { get; set; }
}
}
这是我的索引视图:
<div class="container" style="margin-top:100px; padding-bottom:100px;">
@(Html.Kendo().Grid<Aplikasi_PKPT.Models.Unit>()
.Name("Grid")
.DataSource(datasource => datasource
.Ajax()
.Model(model => model.Id(id => id.UnitID))
.Read(read => read.Action("_Index", "Unit"))
.Create(update => update.Action("_Create","Unit"))
.Update(update => update.Action("_Update", "Unit"))
.Destroy(update => update.Action("_Destroy", "Unit"))
)
.Columns(columns =>
{
columns.Bound(u => u.KodeUnit);
columns.Bound(u => u.NamaUnit);
columns.Bound(u => u.JumlahPegawai);
columns.Command(command => { command.Edit(); command.Destroy(); });
}
)
.ToolBar(toolbar => {
toolbar.Create();
})
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Sortable()
.Filterable()
.Events(e => e.Edit("hideIdField"))
)
<script>
function hideIdField(e) {
$("#UnitID").hide();
$("label[for='UnitID']").hide();
}
</script>
</div>
任何帮助都会被贬低..
答案 0 :(得分:0)
为什么要绑定IEnumerable?
请尝试使用此代码进行创建操作。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Create([DataSourceRequest] DataSourceRequest request,Unit unit)
{
if (unit != null && ModelState.IsValid)
{
db.Unit.Add(u);
db.SaveChanges();
}
return Json(results.ToDataSourceResult(request, ModelState));
}
如果此方法有效,则以与创建操作类似的方式更改更新和删除操作。