Displaying an error from a controller

Topics: Customizing Orchard, General, Writing modules
Aug 8, 2012 at 10:47 AM
Edited Aug 8, 2012 at 10:47 AM

Hi Guys,

If I had a driver that renders a part, the part posts back to a controller and the controller ether redirects to a success page or shows an error. What is the best way to display the original page with an error message?

I guess I could redirect back to the original page with a message in the query string but that seems a but nasty. Is there a better way?

Many thanks

Aug 8, 2012 at 11:24 AM

You could use INotifier to display error messages to the user. Just inject INotifier into your controller and invoke it's Add method.

Aug 8, 2012 at 12:09 PM
Edited Aug 8, 2012 at 1:21 PM

Thanks for the reply Spike. I did as you said then in my controller wrote this:

                    notifier.Add(NotifyType.Error, new LocalizedString(string.Format("Could not find an approved {0} logo.", pdf.Pallet)));
                    return Redirect(Request.UrlReferrer.AbsoluteUri);

But back in my driver, the notifier object is empty. Would it be possible to point me in the right direction?


Aug 8, 2012 at 4:41 PM

Perhaps inject IOrchardServices instead, and use its instance of INotifier. Or, inject Work<INotifier> and then use its Value property (which will by typed INotifier).

Aug 9, 2012 at 9:57 AM

I tried, injecting both of those objects but with no luck I'm afraid; the entry always ends up getting lost during the redirect.

I guess I could use a Javascript postback, unless you have any further ideas?

Thanks anyway

Aug 9, 2012 at 5:41 PM

Well, that makes sense: if you add a notification, then redirect, on the new request you will have a new instance of INotifier. What happens is that at the end of each request, Orchard (or actually the NotifyFilter) reads the entries in INotifier, adds them to TempData, which are then taken to render something into the Messages zone.

So in your case, simply add a notification and redirect to the original page. That original page needs to render the Messages zone, and you're done.

Aug 13, 2012 at 8:23 AM

Ah yes, that makes, scene. I'll try it now, many thanks, much appreciated.