So I set up a simple Kendo Grid, which I want to use to batch edit in cells etc. However when I click Add New Record the site redirects me to http://localhost:52536/Tag/Tag_Read/10322?grid-mode=insert with a page displaying a text list (json?) of my db entries. The in cell editing also doesn't work at all.
I'll post my view and controller below.
I'll post my view and controller below.
View:
@model IEnumerable<HoldAndRelease.Models.Tag>
<br />
<br />
<br />
@(Html.Kendo().Grid(Model) //Bind the grid to ViewBag.Products
.Name("grid")
.Columns(columns =>
{
// Create a column bound to the ProductID property
columns.Bound(tag => tag.TagID);
columns.Bound(tag => tag.ProductionDate);
columns.Bound(tag => tag.CodeDate);
columns.Bound(tag => tag.Amount);
columns.Command(cmd => cmd.Edit());
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(datasource =>
datasource
.Ajax()
.Batch(true)
.PageSize(10)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(tag => tag.TagID);
model.Field(tag => tag.TagID).Editable(false);
})
.Create(create => create.Action("Tag_Create", "Tag"))
.Read(read => read.Action("Tag_Read", "Tag"))
.Update(update => update.Action("Tag_Update", "Tag"))
).Editable(editable => editable.Mode(GridEditMode.InCell))
)
<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>
Controller:
public ActionResult Tag_Read([DataSourceRequest]DataSourceRequest request)
{
db.Configuration.ProxyCreationEnabled = false;
IQueryable<Tag> tags = db.Tags;
DataSourceResult result = tags.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
{
db.Configuration.ProxyCreationEnabled = false;
var entities = new List<Tag>();
if (ModelState.IsValid)
{
foreach (var tag in tags)
{
var entity = new Tag
{
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
};
db.Tags.Add(entity);
entities.Add(entity);
}
db.SaveChanges();
}
return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag
{
TagID = tag.TagID,
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
}), JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag_Update([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
{
db.Configuration.ProxyCreationEnabled = false;
var entities = new List<Tag>();
if (ModelState.IsValid)
{
foreach (var tag in tags)
{
var entity = new Tag
{
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
};
entities.Add(entity);
db.Tags.Attach(entity);
db.Entry(entity).State = EntityState.Modified;
}
db.SaveChanges();
}
return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag
{
TagID = tag.TagID,
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
}), JsonRequestBehavior.AllowGet);
}
@model IEnumerable<HoldAndRelease.Models.Tag>
<br />
<br />
<br />
@(Html.Kendo().Grid(Model) //Bind the grid to ViewBag.Products
.Name("grid")
.Columns(columns =>
{
// Create a column bound to the ProductID property
columns.Bound(tag => tag.TagID);
columns.Bound(tag => tag.ProductionDate);
columns.Bound(tag => tag.CodeDate);
columns.Bound(tag => tag.Amount);
columns.Command(cmd => cmd.Edit());
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(datasource =>
datasource
.Ajax()
.Batch(true)
.PageSize(10)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(tag => tag.TagID);
model.Field(tag => tag.TagID).Editable(false);
})
.Create(create => create.Action("Tag_Create", "Tag"))
.Read(read => read.Action("Tag_Read", "Tag"))
.Update(update => update.Action("Tag_Update", "Tag"))
).Editable(editable => editable.Mode(GridEditMode.InCell))
)
<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>
Controller:
public ActionResult Tag_Read([DataSourceRequest]DataSourceRequest request)
{
db.Configuration.ProxyCreationEnabled = false;
IQueryable<Tag> tags = db.Tags;
DataSourceResult result = tags.ToDataSourceResult(request);
return Json(result, JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
{
db.Configuration.ProxyCreationEnabled = false;
var entities = new List<Tag>();
if (ModelState.IsValid)
{
foreach (var tag in tags)
{
var entity = new Tag
{
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
};
db.Tags.Add(entity);
entities.Add(entity);
}
db.SaveChanges();
}
return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag
{
TagID = tag.TagID,
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
}), JsonRequestBehavior.AllowGet);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Tag_Update([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Tag> tags)
{
db.Configuration.ProxyCreationEnabled = false;
var entities = new List<Tag>();
if (ModelState.IsValid)
{
foreach (var tag in tags)
{
var entity = new Tag
{
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
};
entities.Add(entity);
db.Tags.Attach(entity);
db.Entry(entity).State = EntityState.Modified;
}
db.SaveChanges();
}
return Json(entities.ToDataSourceResult(request, ModelState, tag => new Tag
{
TagID = tag.TagID,
ProductionDate = tag.ProductionDate,
CodeDate = tag.CodeDate,
Amount = tag.Amount
}), JsonRequestBehavior.AllowGet);
}