Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Daniel Rogers 134 posts 712 karma points
    Feb 02, 2022 @ 13:13
    Daniel Rogers
    0

    Creating a Helper file and passing the controller to it.

    I are trying to tidy up some code and not sure hot raise the question.

    So I have a surface controller that I want to call some common code (helper) in another file. I need to pass the UmbracoHelper as I need to access the dictionary and IPublishedContent from inside the helper routine.

    SO I think I have this sorted out but I also need to pass HttpRequestBase Controller as well.

    This is what I have so fare in my Surface Controller

    public class IBDFormsRenderController : SurfaceController
        {
            private IBDEmailHelper _emailHelper
            {
                get
                {
                    return new IBDEmailHelper(Current.UmbracoHelper, Controller);
                }
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult AjaxSubmitWorkshopForm(IBDWorkshopFormModel Model)
            {
    .............
                if (ModelState.IsValid)
                {
                     var captureResult = _emailHelper.GoogleCapture(GS_ID, Model.ID);
                     if (!string.IsNullOrEmpty(captureResult)) { return JavaScript(captureResult); }
                }
    ..........................
        return JavaStript("hello")
    }
    

    Helper

    public class IBDEmailHelper
        {
            private UmbracoHelper _uHelper { get; set; }
            private Controller _uController { get; set; }
    
            /// <summary>
            /// Default constructor for SearchHelper
            /// </summary>
            /// <param name="uHelper">An umbraco helper to use in your class</param>
            public IBDEmailHelper(UmbracoHelper uHelper, Controller uController)
            {
                _uHelper = uHelper;
                _uController = uController;
            }
    
            public string GoogleCapture(int GS_ID, string ID)
            {
                IPublishedContent globalSettings = (IPublishedContent)_uHelper.Content(GS_ID);
    ........................... 
                        var response = _uController.Request["g-recaptcha-response"];
    ..........................
                }
                return String.Empty;
            }
    

    Not not all code is here for sake of space but there is the 2 key lines.

    The visual studio has no issues with the code that is in the helper however the line

     return new IBDEmailHelper(Current.UmbracoHelper, Controller);
    

    in the controller has an issue with "Controller" it gives the following issue

    CS1109: 'Controller' is a type which is not valid in the given context

    also noted that VS thinks this is a System.Web.Mvc.Controller but I think it needs o be HttpRequestBase Controller

    If someone can point me in the right direction would be great.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Feb 02, 2022 @ 13:58
    Søren Gregersen
    100

    Hi,

    Instead of passing a type name (Controller), you should pass the instance

    you have:

     return new IBDEmailHelper(Current.UmbracoHelper, Controller);
    

    you need:

    return new IBDEmailHelper(Current.UmbracoHelper, this);
    

    It is not a very good practice to create a new instance every time you get a property ( the code in { get {...} } ).

    I would suggest remove the property, and just create a variable with the helper when needed.

    The body of you action will then look something like:

    if (ModelState.IsValid)
    {
        var emailHelper = new IBDEmailHelper(Current.UmbracoHelper, this);
        var captureResult = _emailHelper.GoogleCapture(GS_ID, Model.ID);
    
        if (!string.IsNullOrEmpty(captureResult)) 
        {
            return JavaScript(captureResult); 
        }
    }
    

    HTH :)

  • Daniel Rogers 134 posts 712 karma points
    Feb 02, 2022 @ 14:16
    Daniel Rogers
    0

    Thanks Soren worked a treat.

Please Sign in or register to post replies

Write your reply to:

Draft