How to use dashes in HTML

I am trying to use HTML5 data- attributes in my ASP.NET MVC 1 project. (I am a C# and ASP.NET MVC newbie.)

 <%= Html.ActionLink("« Previous", "Search",
     new { keyword = Model.Keyword, page = Model.currPage - 1},
     new { @class = "prev", data-details = "Some Details"   })%>

The "data-details" in the above htmlAttributes give the following error:

 CS0746: Invalid anonymous type member declarator. Anonymous type members 
  must be declared with a member assignment, simple name or member access.

It works when I use data_details, but I guess it need to be starting with "data-" as per the spec.

My questions:

  • Is there any way to get this working and use HTML5 data attributes with Html.ActionLink or similar Html helpers ?
  • Is there any other alternative mechanism to attach custom data to an element? This data is to be processed later by JS.

  • Update: MVC 3 and newer versions have built-in support for this. See JohnnyO's highly upvoted answer below for recommended solutions.

    I do not think there are any immediate helpers for achieving this, but I do have two ideas for you to try:

    // 1: pass dictionary instead of anonymous object
    <%= Html.ActionLink( "back", "Search",
        new { keyword = Model.Keyword, page = Model.currPage - 1},
        new Dictionary<string,Object> { {"class","prev"}, {"data-details","yada"} } )%>
    
    // 2: pass custom type decorated with descriptor attributes
    public class CustomArgs
    {
        public CustomArgs( string className, string dataDetails ) { ... }
    
        [DisplayName("class")]
        public string Class { get; set; }
        [DisplayName("data-details")]
        public string DataDetails { get; set; }
    }
    
    <%= Html.ActionLink( "back", "Search",
        new { keyword = Model.Keyword, page = Model.currPage - 1},
        new CustomArgs( "prev", "yada" ) )%>
    

    Just ideas, haven't tested it.


    This problem has been addressed in ASP.Net MVC 3. They now automatically convert underscores in html attribute properties to dashes. They got lucky on this one, as underscores are not legal in html attributes, so MVC can confidently imply that you'd like a dash when you use an underscore.

    For example:

    @Html.TextBoxFor(vm => vm.City, new { data_bind = "foo" })
    

    will render this in MVC 3:

    <input data-bind="foo" id="City" name="City" type="text" value="" />
    

    If you're still using an older version of MVC, you can mimic what MVC 3 is doing by creating this static method that I borrowed from MVC3's source code:

    public class Foo {
        public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) {
            RouteValueDictionary result = new RouteValueDictionary();
            if (htmlAttributes != null) {
                foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes)) {
                    result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                }
            }
            return result;
        }
    }
    

    And then you can use it like this:

    <%: Html.TextBoxFor(vm => vm.City, Foo.AnonymousObjectToHtmlAttributes(new { data_bind = "foo" })) %>
    

    and this will render the correct data-* attribute:

    <input data-bind="foo" id="City" name="City" type="text" value="" />
    

    It's even easier than everything suggested above. Data attributes in MVC which include dashes (-) are catered for with the use of underscore (_).

    <%= Html.ActionLink("« Previous", "Search",
     new { keyword = Model.Keyword, page = Model.currPage - 1},
     new { @class = "prev", data_details = "Some Details"   })%>
    

    I see JohnnyO already mentioned this.

    链接地址: http://www.djcxy.com/p/23358.html

    上一篇: 允许在连接asp.net mvc 2控制器名称的连字符中使用连字符

    下一篇: 如何在HTML中使用破折号