When inserting a new record or updating the Kendo Grid I get this error.
My Controller
My View
@model IEnumerable<KaringBasico.Models.actividades_economicas>
@{
ViewBag.Title = "Actividades Economicas";
Layout = "~/Views/Shared/_LayoutKendo.cshtml";
}
<h5><strong>@ViewBag.Title.</strong></h5>
@(Html.Kendo().Grid(Model)
.Name("gridActividadesEconomicas")
.Columns(columns =>
{
columns.Bound(p => p.actividad_economica).Width(160);
columns.Bound(p => p.descripcion).Title("Descripción");
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(210);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Resizable(resize => resize.Columns(true))
.ColumnMenu()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.actividad_economica))
.Create(update => update.Action("_Create", "ActividadesEconomicas"))
.Read(read => read.Action("_Read", "ActividadesEconomicas"))
.Update(update => update.Action("_Update", "ActividadesEconomicas"))
.Destroy(update => update.Action("_Destroy", "ActividadesEconomicas"))
)
)
<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>
My Controller
using System;
using System.Linq;
using System.Web.Mvc;
using KaringBasico.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using KaringHelper;
namespace KaringBasico.Controllers
{
public class ActividadesEconomicasController : Controller
{
private readonly IUnitOfWork _uow;
private readonly IRepository<actividades_economicas> selectedRepository;
public ActividadesEconomicasController()
{
_uow = new UnitOfWork<BDKaringDataContext>();
selectedRepository = _uow.GetRepository<actividades_economicas>();
}
public ActionResult Index()
{
ViewData["titulo_pagina"] = "Actividades Economicas";
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Create([DataSourceRequest] DataSourceRequest request, actividades_economicas tabla)
{
try
{
if (TryUpdateModel(tabla))
{
selectedRepository.Add(tabla);
_uow.Save();
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Update([DataSourceRequest] DataSourceRequest request, string actividad_economica)
{
try
{
actividades_economicas tabla = selectedRepository.SearchFor(s => s.actividad_economica == actividad_economica).SingleOrDefault();
if (tabla != null)
{
if (TryUpdateModel(tabla))
{
_uow.Save();
}
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Destroy([DataSourceRequest] DataSourceRequest request, string actividad_economica)
{
try
{
actividades_economicas tabla = selectedRepository.SearchFor(s => s.actividad_economica == actividad_economica).SingleOrDefault();
if (tabla != null)
{
selectedRepository.Delete(tabla);
_uow.Save();
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
public ActionResult _Read([DataSourceRequest] DataSourceRequest request)
{
return Json(selectedRepository.GetAll().ToDataSourceResult(request));
}
}
}
using System.Linq;
using System.Web.Mvc;
using KaringBasico.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using KaringHelper;
namespace KaringBasico.Controllers
{
public class ActividadesEconomicasController : Controller
{
private readonly IUnitOfWork _uow;
private readonly IRepository<actividades_economicas> selectedRepository;
public ActividadesEconomicasController()
{
_uow = new UnitOfWork<BDKaringDataContext>();
selectedRepository = _uow.GetRepository<actividades_economicas>();
}
public ActionResult Index()
{
ViewData["titulo_pagina"] = "Actividades Economicas";
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Create([DataSourceRequest] DataSourceRequest request, actividades_economicas tabla)
{
try
{
if (TryUpdateModel(tabla))
{
selectedRepository.Add(tabla);
_uow.Save();
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Update([DataSourceRequest] DataSourceRequest request, string actividad_economica)
{
try
{
actividades_economicas tabla = selectedRepository.SearchFor(s => s.actividad_economica == actividad_economica).SingleOrDefault();
if (tabla != null)
{
if (TryUpdateModel(tabla))
{
_uow.Save();
}
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _Destroy([DataSourceRequest] DataSourceRequest request, string actividad_economica)
{
try
{
actividades_economicas tabla = selectedRepository.SearchFor(s => s.actividad_economica == actividad_economica).SingleOrDefault();
if (tabla != null)
{
selectedRepository.Delete(tabla);
_uow.Save();
}
}
catch (Exception e)
{
ModelState.AddModelError(string.Empty, e.Message);
}
return Json(new[] { selectedRepository.GetAll() }.ToDataSourceResult(request, ModelState));
}
public ActionResult _Read([DataSourceRequest] DataSourceRequest request)
{
return Json(selectedRepository.GetAll().ToDataSourceResult(request));
}
}
}
@model IEnumerable<KaringBasico.Models.actividades_economicas>
@{
ViewBag.Title = "Actividades Economicas";
Layout = "~/Views/Shared/_LayoutKendo.cshtml";
}
<h5><strong>@ViewBag.Title.</strong></h5>
@(Html.Kendo().Grid(Model)
.Name("gridActividadesEconomicas")
.Columns(columns =>
{
columns.Bound(p => p.actividad_economica).Width(160);
columns.Bound(p => p.descripcion).Title("Descripción");
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(210);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.Resizable(resize => resize.Columns(true))
.ColumnMenu()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.actividad_economica))
.Create(update => update.Action("_Create", "ActividadesEconomicas"))
.Read(read => read.Action("_Read", "ActividadesEconomicas"))
.Update(update => update.Action("_Update", "ActividadesEconomicas"))
.Destroy(update => update.Action("_Destroy", "ActividadesEconomicas"))
)
)
<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>
Uncaught SyntaxError: Unexpected number
at.setter kendo.all.min.js:9
lt.extend._set kendo.all.min.js:11
Vt.extend.accept kendo.all.min.js:11
lt.extend._accept kendo.all.min.js:11
(anonymous
function
) kendo.all.min.js:11
(anonymous
function
) jquery-1.10.2.js:3218
fire jquery-1.10.2.js:3062
self.fireWith jquery-1.10.2.js:3174
deferred.(anonymous
function
) jquery-1.10.2.js:3263
o.(anonymous
function
).call.X.success kendo.all.min.js:11
fire jquery-1.10.2.js:3062
self.fireWith jquery-1.10.2.js:3174
done jquery-1.10.2.js:8249
callback jquery-1.10.2.js:8792
In the provided Grid code, the ID field is as follows:
By default, Unique ID fields should not be editable. They are used in the database and in the Grid's CRUD operations as a unique value identifier to get the specific record to be modified.
However, in the captured screenshot it seems that the first column is not disabled from editing which can cause this issue. You can try to set the [ReadOnly(true)] Data Attribute to the ID field in your Model C# Class definition in order to remedy this problem:
https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.readonlyattribute?view=netframework-4.8