Tuesday, June 26, 2012

Why doesn't ASP.NET MVC include these HtmlHelper methods?

These HtmlHelper extension methods provide typical use methods for creating action links that include areas. It feels kludgy to have to put "area" into the router values object/dictionary. It seems to me that this (or at least something similar along with additional appropriate overloads) should exist in the core implementation.
/// <summary>
/// Returns an anchor element (a element) that contains 
/// the virtual path of the specified area action.
/// </summary>
/// <param name="helper">The HTML helper instance that this method extends.</param>
/// <param name="linkText">The inner text of the anchor element.</param>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="areaName">The name of the area.</param>
/// <returns></returns>
public static MvcHtmlString ActionLink(
 this HtmlHelper helper, 
 string linkText, 
 string actionName, 
 string controllerName, 
 string areaName)
{
 return helper.ActionLink(
  linkText, 
  actionName, 
  controllerName, 
  areaName, 
  null);
}

/// <summary>
/// Returns an anchor element (a element) that contains 
/// the virtual path of the specified area action.
/// </summary>
/// <param name="helper">The HTML helper instance that this method extends.</param>
/// <param name="linkText">The inner text of the anchor element.</param>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="areaName">The name of the area.</param>
/// <param name="htmlAttributes">An object that contains the HTML 
///  attributes to set for the element.</param>
/// <returns></returns>
public static MvcHtmlString ActionLink(
 this HtmlHelper helper, 
 string linkText, 
 string actionName, 
 string controllerName, 
 string areaName, 
 object htmlAttributes)
{
 return helper.ActionLink(
  linkText, 
  actionName, 
  controllerName, 
  new { area = areaName }, 
  htmlAttributes);
}

1 comment:

Salvador Soberanes Pernas said...

I totally agree. I think microsoft should launch a plugin that includes a bunch of html helpers. For example a helper for displaying images, a grid with pagination/sorting, and other bunch of useful stuf. I like MVC because of its clean way to interact with HTML, but that means that most things you have to do them manually.