Accessing controller action through ajax

Topics: Troubleshooting, Writing modules
Jul 24, 2012 at 4:31 PM


I have created a custom module, and now I am trying to access a controller action from jquery.

This is the jquery ajax call:


                data: {model:event, area:'EventManagement'},
                error: function (e) {
                    alert('fail'); //returns 404 every time
                success: function () {
                url: '/Event/QuickAdd'

And here is the controller called EventController (inherits controller):


        public JsonResult QuickAdd(VenueEventQuickAddViewModel model)
            return Json(new { success=true, message = "action reached" });

My module is called 'EventManagement', so I add the area parameter to the ajax call. I also tried removing it and adding a Routes class which I defined as follows, but it too only returned 404s.

public class Routes : IRouteProvider
        public IEnumerable<RouteDescriptor> GetRoutes()
            string area = "EventManagement";
            return new[]{
                new RouteDescriptor{
                    Route=new Route(
                        new RouteValueDictionary {
                            {"controller", "Event"},
                            {"action", "QuickAdd"}
                        new RouteValueDictionary(),
                        new RouteValueDictionary{
                        new MvcRouteHandler())

        public void GetRoutes(ICollection<RouteDescriptor> routes)
            foreach (var route in GetRoutes())


Any suggestions?


Jul 24, 2012 at 4:39 PM

Well, I would suggest not hard-coding the url in the script but that's probably not the problem here. Did you try removing the HttpPost attribute for a moment, just to test, and then try to browse to the action? That would at least validate the action and controller work (if they don't, maybe you forgot to enable the feature?

Jul 24, 2012 at 6:20 PM
Edited Jul 24, 2012 at 6:40 PM

Hi Bertrand

Do you mean enabling my custom module's feature? Because the jquery ajax is called from within the module, so it must be enabled. Or is there another feature that needs to be enabled?

When trying to access the controller directly from the browser it works (if I remove the HttpPostAttribute as you said) as long as I add the JsonRequestBehavior.AllowGet parameter. This is the action I invoke:

        public JsonResult QuickAdd()
            return Json(new { success = true, message = "Event added successfully" }, JsonRequestBehavior.AllowGet);

And I am trying to call it from the browser with this address: http://localhost:30320/OrchardLocal/EventManagement/Event/QuickAdd

As soon as I add the HttpPostAttribute it no longer works.

Jul 24, 2012 at 6:43 PM

Found the issue! My dev environment in visual studio is "http://localhost:30320/OrchardLocal/". So my relative url in the jquery was pointing to the incorrect url (should have been prepended with /OrchardLocal/). Sorry, noob at work.

Jul 26, 2012 at 1:06 PM

Ha! So my suggestion not to hard-code the URL was spot-on ;)

Jul 26, 2012 at 2:07 PM

Could you maybe give an alternative for this situation? Where would I have dynamically retrieved this URL?

Jul 26, 2012 at 2:11 PM

From the Url.Action API?

Jul 26, 2012 at 2:43 PM

Ah. Thanks.