Hi Don,
Apologies for the unclear information.
The Read method can hold an error just as the Update, Create, and Delete. I examined again nthe provided code and I think the problem here is how the error is added to the ModelState. I would suggest change it as follows:
public async Task<ActionResult> GetViewModels([DataSourceRequest]DataSourceRequest request)
{
var models = System.Array.Empty<ViewModel>();
ModelState.AddModelError("ServerError", "Just to test something");
var result = await models.ToDataSourceResultAsync(request, ModelState).ConfigureAwait(false);
return Json(result);
}
This has to be handled on the client. Add the DataSource Error event handler.
.Events(events => events.Error("error_handler"))
..
function error_handler(e) {
console.log("Error Event Fired!");
e.preventDefault();
if (e.status === "customerror") {
alert(e.errors.ServerError.errors[0] , 'Error!');
}
else
{
if (e.errors) {
var message = "There are some errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
var grid = $("#grid").data("kendoGrid");
grid.cancelChanges();
}
</script>
For your convenience, I am attaching a small project demonstrating the above. It is an ASP.NET MVC Core one, however, the above implementation is entirely valid for MVC projects.
I hope this helps.
Regards,
Nikolay
Progress Telerik
Progress is here for your business, like always.
Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.