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.
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);
}
}
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
Helper
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
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.
Hi,
Instead of passing a type name (Controller), you should pass the instance
you have:
you need:
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:
HTH :)
Thanks Soren worked a treat.
is working on a reply...