Skip to content

Instantly share code, notes, and snippets.

@Grinderofl
Created October 27, 2011 09:18
Show Gist options
  • Save Grinderofl/1319129 to your computer and use it in GitHub Desktop.
Save Grinderofl/1319129 to your computer and use it in GitHub Desktop.
C# .NET: ActionLink Helpers for MVC3
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlHelpers.cs" company="">
// Nero Sule
// </copyright>
// <summary>
// HTML Helpers
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Web.HtmlHelpers
{
using System.Web.Mvc;
using System.Web.Mvc.Html;
/// <summary>
/// HTML Helpers
/// </summary>
public static class HtmlHelpers
{
/// <summary>
/// Formats a menu link for action.
/// </summary>
/// <param name="htmlHelper">
/// The html helper.
/// </param>
/// <param name="linkText">
/// The link text.
/// </param>
/// <param name="actionName">
/// The action name.
/// </param>
/// <param name="controllerName">
/// The controller name.
/// </param>
/// <param name="className">
/// The class name to be appended to the link.
/// </param>
/// <returns>
/// Formatted HTML String
/// </returns>
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string className)
{
string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
if (actionName == currentAction && controllerName == currentController)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = className });
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
/// <summary>
/// Formats a Menu Link for controller rather than action.
/// </summary>
/// <param name="htmlHelper">
/// The html helper.
/// </param>
/// <param name="linkText">
/// The link text.
/// </param>
/// <param name="actionName">
/// The action name.
/// </param>
/// <param name="controllerName">
/// The controller name.
/// </param>
/// <param name="className">
/// The class name to be attached to the link.
/// </param>
/// <returns>
/// Formatted menu link
/// </returns>
public static MvcHtmlString MenuLinkForController(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string className)
{
string currencController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
if (controllerName == currencController)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = className });
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
/// <summary>
/// Action link, if longer than set amount of characters, shortened to that amount and three dots added.
/// </summary>
/// <param name="htmlHelper">
/// The html helper.
/// </param>
/// <param name="linkText">
/// The link text.
/// </param>
/// <param name="actionName">
/// The action name.
/// </param>
/// <param name="controllerName">
/// The controller name.
/// </param>
/// <param name="routeValues">
/// Route values
/// </param>
/// <param name="arguments">
/// The arguments.
/// </param>
/// <param name="places">
/// The places.
/// </param>
/// <returns>
/// Formatted string
/// </returns>
public static MvcHtmlString ShortActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object routeValues = null,
object arguments = null,
int places = 8)
{
var text = linkText;
if (linkText.Length >= places)
{
text = linkText.Substring(0, places) + "...";
}
return htmlHelper.ActionLink(text, actionName, controllerName, routeValues, arguments);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment