【问题标题】:Using HTML5 "data-" attributes with MVC3 and Html.ActionLink<TController>使用带有 MVC3 和 Html.ActionLink<TController> 的 HTML5“数据-”属性
【发布时间】:2012-01-04 16:03:18
【问题描述】:

因此,Html.ActionLink() 的非泛型重载似乎可以很好地与 HTML5 data- 属性配合使用,方法是将带下划线的属性重命名为带连字符的属性:

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

但是,这似乎不适用于强类型 Html.ActionLink&lt;TController&gt;()

那么,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 的重载,因此该路由已失效。

【问题讨论】:

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


    【解决方案1】:

    看来HtmlHelper.ActionLink&lt;TController&gt; 上的Microsoft.Web.Mvc 扩展方法中存在错误(功能?)。我的解决方法是:

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

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

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多