Extend the Controller in Custom Forms

Topics: Customizing Orchard
Sep 11, 2014 at 5:49 PM
Edited Sep 11, 2014 at 5:53 PM
Hi!

I build an contact flyout with custom forms for my webpage. This form needs to be send async to the server.
Image
With the help of this article its no problem.
I parse the required information from the form and send it with an ajax request, but I am not happy with the return. Custom Forms return, as needed without ajax, the whole html, but I need an json return or something like that. The sloppy workaround is to edit the custom form module with this code and add an attribute in my ajax call:
if (HttpContext.Request["isAjax"] != null && HttpContext.Request["isAjax"] == "true")
 {
     return Json(model);
} 
But I am not really a fan of editing other modules because in the next update it will break again. So I want to ask, if there is a way to extend Custom Forms in this case?

edit: How can I change the size of the picture in markdown? :)
Sep 11, 2014 at 7:37 PM
Edited Sep 11, 2014 at 7:38 PM
I think you need to copy the code and add it to your custom controller. Tweak it as you need in there.

Then you need to define a new route for your custom controller with a higher priority (means lower priority number, the lower the priority number the higher the priority ;) ) then the CustomForms controller does.

Maybe (I'm not sure) you also need to use the OrchardSuppressDependency attribute on your custom controller:
[OrchardSuppressDependency("Orchard.CustomForms.Controllers.TheControllerToOverride")]
public class YourOverridingController() {
    // Your tweaked code
    if (HttpContext.Request["isAjax"] != null && HttpContext.Request["isAjax"] == "true")
    {
        return Json(model);
    }
}