2011-11-23 28 views
3

属性,所以看起来的Html.ActionLink()作品很好地与HTML5通过重命名为下划线的属性到属性与连字符data-属性的非泛型重载:使用HTML5“数据 - ”与MVC3和Html.ActionLink <TController>

How to use dashes in HTML-5 data-* attributes in ASP.NET MVC

但是,这似乎不适用于强类型Html.ActionLink<TController>()

所以,链接,jQuery Mobile的

@(Html.ActionLink<HomeController>(
    c => c.Index(), 
    "Home", 
     new { data_direction="reverse" })) 

给出

<a data_direction="reverse" href="/" class="ui-link">Home</a> 

的HTML源代码是不是我想要的。

任何想法?没有超载需要RouteValueDictionary,以便路由不可用。

回答

4

所以在HtmlHelper.ActionLink<TController>Microsoft.Web.Mvc扩展方法中似乎有一个bug(功能?)。我的解决方法是:

using System; 
using System.Linq.Expressions; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Html; 
using System.Web.Routing; 
using Authentication; 

public static class LinkExtensions 
{ 

    // Named thusly to avoid conflict, I anticipate a search-and-replace later! 
    public static MvcHtmlString ActionLink5<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText, object htmlAttributes) where TController : Controller 
    { 
     RouteValueDictionary routeValuesFromExpression = Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression<TController>(action); 
     return helper.RouteLink(linkText, routeValuesFromExpression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); 
    } 
} 

当其与所谓的

@(Html.ActionLink5<HomeController>(
    c => c.Index(), 
    "Home", 
    new { data_direction="reverse" })) 

似乎工作就好了... ...