ASP.Net MVC 3, Kendo UI Web.
Typical example — there is a database and some tables, user is editing the data — adding new entries, deleting, updating.
Apart from technical problems (network connection down), there could be several logical ones — for example user does not have permission to write anything, or there is a lock on data, or user tries to update something what was deleted meanwhile, and so on.
So I would like to return in such cases an error, so I could handle it with Kendo and display, let’s say some alert dialog (JS alert).
Now — what would be the most well designed way, most correct, to do so? I am not looking for “oh, well, it just works” approach.
Is throwing HttpException a good idea in such case (I have my issues with it, because it is http exception, and the error I try to pass has nothing to do with http really)?
I’m going to work under assumption that you’re using Kendo DataSource object which exists on most widgets in self-binding mode (has transport section defined and fires it’s own CRUD requests).
The actions that your grid’s CRUD is tied to, should be returning IEnumerable.ToDataSourceResult, on the model it received as input. If in your processing you detected errors, add them to ModelState via ModelState.AddModelError method which you can access in your action. The ToDataSourceResult will detect presense of any errors in ModelState object automatically and serialize them into the JSON response (errors field). You can then act on them via subscribing to DataSource.error even on the client side (see http://docs.kendoui.com/api/framework/datasource#error).