27

is it possible to use dash (-) in a member name of an anonymous class? I'm mainly interested in this to use with asp.net mvc to pass custom attributes to html-helpers, since I want my html to pass html5-validation, this starting with data-.

Exemple that doesn't work:

<%=Html.TextBoxFor(x => x.Something, new {data-animal = "pony"})%>

Putting a @ in front of the member name doesn't do the trick either.

Update: If this isn't possible, is there a recommended way todo what I want? My current temporary solution is to add a replace to the whole thing like this:

<%=Html.TextBoxFor(x => x.Something, new {data___animal = "pony"}).Replace("___", "-")%>

But that sucks, because it's ugly and will break when Model.Something contains three underscores. Buhu.

1
  • Your workaround doesn't work in all scenarios: Html.DroppableZone(Zones.Left).WrapIn("div", new { @data-role = "collapsible"}).Render(); does not return a string over which one can replace anything :-( Commented Jun 9, 2012 at 23:37

5 Answers 5

26

Just found this post while searchching for the same problem.

I found this link: http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Using-custom-data-attributes-in-ASPNET-MVC.aspx

It resolves the problem. It mentions the following:

[...] or better yet, just use code from ASP.NET MVC source:

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;
}
3
  • 15
    From the link: "Fortunately a solution for this was added in ASP.NET MVC 3 so that you can use an underscore instead which will be automatically converted to a hyphen."
    – joelsand
    Commented Jun 28, 2011 at 2:15
  • 1
    You don't have to do this as @joelsand said. More here stackoverflow.com/a/9444822/245495
    – Odys
    Commented Apr 28, 2014 at 11:53
  • 2
    Well that sucks if you want to use an underscore.
    – Triynko
    Commented Oct 15, 2015 at 14:42
17

Collecting the Asp-Mvc version specific ways to do data- here:

MVC 3+ : Use an underscore _ and it will be automatically replaced by mvc
MVC 1?,2: see @Jean-Francois answer, which points to this

0
2

No, because the dash is a C# operator (minus), and white space isn't significant.

Right now the compiler thinks you are trying to subtract animal from data, which doesn't work unless the - operator is specified for the types in question.

1

It is not possible to use - as part of any identifier. http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx

1
  • 3
    This answer is true but incomplete. There's a workaround. Please see my answer on this question. Commented Jul 21, 2011 at 13:00
0

No, you can't use the hyphen character. You need to use alphanumeric characters, underscores, or the other characters described here. '-' is treated as a minus. data-animal would be treated the same as data - animal, so that won't even compile unless you have separately defined data and animal (and it could present subtle bugs if you have!).

Edit: With C#'s capability to have identifiers with Unicode escape sequences, you can get the effect of a dash in an identifier name. The escape sequence for "&mdash" (the longer dash character) is "U+2014". So you would express data-animal as data\u2014animal. But from a coding style point of view, I'm not sure why you wouldn't choose a more convenient naming convention.

Also, another point to highlight from "2.4.2 Identifiers (C#)": You can't have two of these escape sequences back to back in an identifier (e.g. data\u2014\u2014animal).

5
  • The naming convention is from HTML5; dev.w3.org/html5/spec/… Commented Mar 5, 2010 at 19:06
  • Ps yeah, \u2014 is like eh not optimal. Commented Mar 5, 2010 at 19:07
  • \u002d is the hyphen character. When I saw "dash" in the question, I thought of the "&mdash" character first.
    – David
    Commented Mar 5, 2010 at 19:18
  • @svinto: You're right, \u2014 is not optimal. I was just pointing out that you can in fact use it in an identifier if you want the dash character. That said, even if you did have a way to make these \u* identifiers more human-readable, they would look the same as the characters ('-', etc.) that aren't allowed in identifiers (unless the syntax was highlighted differently, but admittedly I am not well-versed in that).
    – David
    Commented Mar 5, 2010 at 19:22
  • Nope... new { @data\u002drole = "collapsible"} does not compile. Commented Jun 9, 2012 at 23:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.