在MVC3和Html.ActionLink <tcontroller> </tcontroller>中使用HTML5“data-”属性

时间:2011-11-23 10:51:37

标签: c# asp.net-mvc-3 model-view-controller html5

通过将带下划线的属性重命名为带连字符的属性,似乎Html.ActionLink()的非泛型重载与HTML5 data-属性很好地协同工作:

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" } ))

提供

的HTML源代码
<a data_direction="reverse" href="/" class="ui-link">Home</a>

这不是我想要的。

有什么想法吗?没有超载需要RouteValueDictionary以便路线出来。

1 个答案:

答案 0 :(得分:4)

所以Microsoft.Web.Mvc上的HtmlHelper.ActionLink<TController>扩展方法似乎存在错误(功能?)。我的解决方法是:

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" } ))

似乎工作得很好......

相关问题