1

my actual example scenario is:

I have a Controller with a Action named Create(TClass obj).

When an error occurs, I wish fire an specialized exception (MyException), but I don't wanna to redirect to another page (General Page Error Handler, or others). I want to stay in the same page and show the message error in a jQueryUI Dialog popup.

I've tried these options:

  • Override the method OnException (in Controller)
  • Tested PostSharp (specializing OnExceptAspect)
  • Tested the solution in this link and others like.

Have you any idea?

Thanks.

Community
  • 1
  • 1
Silvio Delgado
  • 6,798
  • 3
  • 18
  • 22
  • It is possible if all your requests to server are Ajax requests. In this case you add special onError function to all ajax requests that catch your exception. – Kirill Bestemyanov Sep 05 '12 at 17:19
  • Unfortunately, most requests are by post, not by Ajax... :( – Silvio Delgado Sep 05 '12 at 17:39
  • In this case, you cannot do what you want. When Exception is thrown there are no your page. You need to catch your exception in controller where it thrown and return that controller view that must show your jqueryUI Dialog. – Kirill Bestemyanov Sep 05 '12 at 18:04

1 Answers1

1

Throughout your application in all your controller methods, call:

catch(Exception e)
{
    errorModel errorModelInstance = new errorModel();
    errorModel.message = e.Message;
    errorModel.stack = e.StackTrace;  //You get the idea...
    return PartialView("someErrorView", errorModelinstance);
}

Then, have a someErrorView.cshtml file which contains the script reference for jQueryUI for the modal popup (unless your master layout already references it throughout the application), and have initializers for the modal popup in that partial view. (Let me know if you want the front-end code for initializing the popup).

This partial view will return the model "errorModel". You will have to define a class under your models folder to declare the string properties of the errorModel.

Code Monkey
  • 643
  • 6
  • 18