This is a migrated thread and some comments may be shown as answers.

[Solved] How to do error handling in [GridAction] methods?

8 Answers 374 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
dizzwave
Top achievements
Rank 1
dizzwave asked on 12 Apr 2011, 09:53 PM
Hello,

How can I do error handling in a [GridAction] method?

I am doing Ajax databinding on a grid, and want to be able to tell the user why one of their inserts might have failed.
If I use the MVC [HandleError] attribute on the [GridAction] method, it doesn't work. It defaults to the "500 - Internal Server Error" popup. If I try to hand code it like so:

catch (Exception ex)
            {
                ViewData["Description"] = ex.ToString();
                return View("Error");
            }

then I get the error popup "Error! The requested URL did not return JSON."

What are my options here?

Thank you,
Dave

8 Answers, 1 is accepted

Sort by
0
James Strope
Top achievements
Rank 1
answered on 25 Apr 2011, 02:29 PM
I, too, have this same issue (though mine involves a retrieval and not an update).  Since the AJAX call expects a JSON representation of the grid, you cannot include ViewData or access anything on the parent View.  You also can't return a RedirectToAction.

Ideas, anyone?
0
Atanas Korchev
Telerik team
answered on 25 Apr 2011, 02:47 PM
Hello,

 To handle errors one must subscribe to the OnError JavaScript event. It is raised when there is an exception, timeout or unexpected response.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Strope
Top achievements
Rank 1
answered on 25 Apr 2011, 06:34 PM
This sounds promising, Atanas.  But, how do I throw the error so that the script can catch it?

Here is my test controller code:

       

[Telerik.Web.Mvc.GridAction]
public ActionResult _Index(string strKey)
{
    NotesGridViewModel viewModel = new NotesGridViewModel(strKey);
    viewModel.FillNotesList();
    //if something goes wrong in FillNotesList, then what?
    return View(new Telerik.Web.Mvc.GridModel(viewModel.NoteRows));
}
0
dizzwave
Top achievements
Rank 1
answered on 26 Apr 2011, 12:18 AM
James, thanks for the extra info.

Atanas, I too would like to see how to do this.. I look forward to your reply!

thank you,
Dave
0
Atanas Korchev
Telerik team
answered on 26 Apr 2011, 08:37 AM
Hi,

 How do would you deal with this situation if the grid weren't involved? I think the same approach can be used here as well. For example you can throw an exception. The grid will show the default error message that 500 code was returned (server error). You can use the OnError event to handle this situation.

All the best,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Strope
Top achievements
Rank 1
answered on 26 Apr 2011, 12:44 PM
That was the first thing I tried.  If I throw an exception, the web site crashes with an unhandled exception.  It doesn't get far enough to pass anything back to the client so that the javascript event can trigger.

If the grid were not involved, I would catch the exception in server code and populate something in my View, but that does not apply here, as I do not have access to the parent View.
0
Atanas Korchev
Telerik team
answered on 26 Apr 2011, 01:06 PM
Hi James Strope,

 This is not expected. If there is unhandled exception during an Ajax request your web site would either show the default exception screen or redirect to a new page. In both cases unexpected response would be returned to the grid and the OnError event should be raised. I am sending a sample application which demonstrates the same.

You can also use the model state as demonstrate in this online example (check the controller and the way the OnError event is handled).

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James Strope
Top achievements
Rank 1
answered on 26 Apr 2011, 03:08 PM
Ah, you are correct.  It's the IDE alerting me that there is an unhandled exception, but once I hit F5, it returns to the client and is caught by the grid as expected.  Thanks for the quick response Atanas.
Tags
Grid
Asked by
dizzwave
Top achievements
Rank 1
Answers by
James Strope
Top achievements
Rank 1
Atanas Korchev
Telerik team
dizzwave
Top achievements
Rank 1
Share this question
or