Skip to content

Instantly share code, notes, and snippets.

@kevinbrechbuehl
Created November 16, 2013 16:25
Show Gist options
  • Select an option

  • Save kevinbrechbuehl/7502137 to your computer and use it in GitHub Desktop.

Select an option

Save kevinbrechbuehl/7502137 to your computer and use it in GitHub Desktop.
public class UnicHelper : Sitecore.Mvc.Helpers.SitecoreHelper
{
/// <summary>
/// The controller field name
/// </summary>
public const string ControllerFieldName = "fhController";
/// <summary>
/// The action field name
/// </summary>
public const string ActionFieldName = "fhAction";
/// <summary>
/// Initializes a new instance of the <see cref="UnicHelper"/> class.
/// </summary>
/// <param name="htmlHelper">The HTML helper.</param>
public UnicHelper(HtmlHelper htmlHelper)
: base(htmlHelper)
{
}
/// <summary>
/// Forms the handler.
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="action">The action.</param>
/// <returns>Needed additional fields</returns>
public override HtmlString FormHandler(string controller, string action)
{
// get the current controller
if (string.IsNullOrWhiteSpace(controller))
{
controller = this.GetValueFromCurrentRendering("Controller");
if (controller != null && controller.Contains(","))
{
controller = controller.Split(',').First().Split('.').Last().Trim();
}
if (!string.IsNullOrWhiteSpace(controller) && !controller.Contains("Controller"))
{
controller += "Controller";
}
}
// get the current action
if (string.IsNullOrWhiteSpace(action))
{
action = this.GetValueFromCurrentRendering("Controller Action");
}
// empty result
if (string.IsNullOrWhiteSpace(controller) || string.IsNullOrWhiteSpace(action))
{
return new HtmlString(string.Empty);
}
// append the hidden fields
return new HtmlString(
this.HtmlHelper.Hidden(ControllerFieldName, controller).ToString()
+ this.HtmlHelper.Hidden(ActionFieldName, action).ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment