【问题标题】:ActionLink htmlAttributesActionLink html属性
【发布时间】:2011-05-05 18:26:57
【问题描述】:

作品

<a href="@Url.Action("edit", "markets", new { id = 1 })" 
            data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>

不起作用 - 为什么?

@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})

您似乎无法将 data-icon="gear" 之类的内容传递给 htmlAttributes?

建议?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 actionlink html.actionlink


    【解决方案1】:

    问题是您的匿名对象属性data-icon 的名称无效。 C# 属性的名称中不能有破折号。有两种方法可以解决这个问题:

    使用下划线代替破折号(MVC 会在发出的 HTML 中自动将下划线替换为破折号):

    @Html.ActionLink("Edit", "edit", "markets",
          new { id = 1 },
          new {@class="ui-btn-right", data_icon="gear"})
    

    使用接受字典的重载:

    @Html.ActionLink("Edit", "edit", "markets",
          new { id = 1 },
          new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });
    

    【讨论】:

    • 下划线似乎不适用于Ajax.ActionLink helpers
    • 下划线技巧听起来很奇怪,如果你想在 html 属性中添加下划线怎么办?
    • @MichielReyers 你可以使用字典中的重载
    • .net Core Tag Helpers 解决了所有这些问题 - 来自未来。
    【解决方案2】:

    用下划线替换所需的连字符;它将自动呈现为连字符:

    @Html.ActionLink("Edit", "edit", "markets",
        new { id = 1 },
        new {@class="ui-btn-right", data_icon="gear"})
    

    变成:

    <form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>
    

    【讨论】:

      【解决方案3】:
      @Html.ActionLink("display name", "action", "Contorller"
          new { id = 1 },Html Attribute=new {Attribute1="value"})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多