I'm attempting to add a handler for errors to an MVC grid wrapper. My grid gets its data and does editing via AJAX. That is working properly. I'm now trying to add support for authorization/roles so I want to be able to show the user a message when they don't have access and, for example, click on the Edit button.
All the samples I've seen use events.error. So I created a grid as below with a javascript function as below. However, when trying to show the view in the browser I get a compilation error in the browser "'Kendo.Mvc.UI.Fluent.GridEventBuilder' does not contain a definition for 'Error' and no extension method 'Error' accepting a first argument of type 'Kendo.Mvc.UI.Fluent.GridEventBuilder' could be found (are you missing a using directive or an assembly reference?)"
I'm sure I must be missing something simple. Can anyone offer assistance in where I've gone off the tracks? At least as helpful, can you point me to the correct spot in the documentation to find this information?
@(Html.Kendo().Grid<KendoMVC4.Models.Power>()
.Name("grid")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("ExecutedPowers_Index", "Powers")) // Set the action method which will return the data in JSON format
.PageSize(10)
.Model(model => model.Id(p => p.ID))
.Create(update => update.Action("Power_Create", "Powers"))
.Update(update => update.Action("Power_Update", "Powers"))
.Destroy(update => update.Action("Delete", "Powers"))
)
.Columns(columns =>
{
columns.Bound(p => p.PowerNumber);
columns.Bound(p => p.Status);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
columns.Command(command => { command.Custom("Forfeit").Click("showForfeit"); });
})
.Pageable()
.Sortable()
.ToolBar(toolbar => toolbar.Create())
.Editable(ed => ed.Mode(GridEditMode.PopUp).TemplateName("PowerEditor")
.Enabled(User.IsInRole("PowerEditor"))
.Window(w => w.Title("Edit Power Details"))
.Window(w => w.Width(560))
.Window(w => w.Height(750))
)
.ClientDetailTemplateId("client-powerDetails")
.Events(events => events.Error(@<text>function(e) { $.proxy(error, $("#Grid").data("kendoGrid"))(e); }</text>))
)
<script type="text/javascript">
function grid_errorHandler(e) {
if (e.errors) {
var message = "Errors:\n\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n\n";
});
}
});
alert(message);
}
else if (e.xhr.status == 401) {
//show message
var grid = $("#grid").data("kendoGrid");
grid.table.on("click", "tbody > tr", function (e) {
e.stopPropagation();
});
grid.element.find("> .k-grid-toolbar").hide();
}
}
</script>
All the samples I've seen use events.error. So I created a grid as below with a javascript function as below. However, when trying to show the view in the browser I get a compilation error in the browser "'Kendo.Mvc.UI.Fluent.GridEventBuilder' does not contain a definition for 'Error' and no extension method 'Error' accepting a first argument of type 'Kendo.Mvc.UI.Fluent.GridEventBuilder' could be found (are you missing a using directive or an assembly reference?)"
I'm sure I must be missing something simple. Can anyone offer assistance in where I've gone off the tracks? At least as helpful, can you point me to the correct spot in the documentation to find this information?
@(Html.Kendo().Grid<KendoMVC4.Models.Power>()
.Name("grid")
.DataSource(dataSource => dataSource // Configure the grid data source
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("ExecutedPowers_Index", "Powers")) // Set the action method which will return the data in JSON format
.PageSize(10)
.Model(model => model.Id(p => p.ID))
.Create(update => update.Action("Power_Create", "Powers"))
.Update(update => update.Action("Power_Update", "Powers"))
.Destroy(update => update.Action("Delete", "Powers"))
)
.Columns(columns =>
{
columns.Bound(p => p.PowerNumber);
columns.Bound(p => p.Status);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
columns.Command(command => { command.Custom("Forfeit").Click("showForfeit"); });
})
.Pageable()
.Sortable()
.ToolBar(toolbar => toolbar.Create())
.Editable(ed => ed.Mode(GridEditMode.PopUp).TemplateName("PowerEditor")
.Enabled(User.IsInRole("PowerEditor"))
.Window(w => w.Title("Edit Power Details"))
.Window(w => w.Width(560))
.Window(w => w.Height(750))
)
.ClientDetailTemplateId("client-powerDetails")
.Events(events => events.Error(@<text>function(e) { $.proxy(error, $("#Grid").data("kendoGrid"))(e); }</text>))
)
<script type="text/javascript">
function grid_errorHandler(e) {
if (e.errors) {
var message = "Errors:\n\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n\n";
});
}
});
alert(message);
}
else if (e.xhr.status == 401) {
//show message
var grid = $("#grid").data("kendoGrid");
grid.table.on("click", "tbody > tr", function (e) {
e.stopPropagation();
});
grid.element.find("> .k-grid-toolbar").hide();
}
}
</script>