2011-11-23 45 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" })) 

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