Dash (-) in anonymous class member
Asked Answered
L

5

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.

Lantha answered 5/3, 2010 at 18:32 Comment(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 :-(Regolith
Z
1

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

Zurheide answered 5/3, 2010 at 18:35 Comment(1)
This answer is true but incomplete. There's a workaround. Please see my answer on this question.Lucan
L
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;
}
Lucan answered 8/4, 2011 at 15:41 Comment(3)
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."Williawilliam
You don't have to do this as @Williawilliam said. More here https://mcmap.net/q/173958/-how-to-specify-data-attributes-in-razor-e-g-data-externalid-quot-23151-quot-on-this-html-checkboxforDeadpan
Well that sucks if you want to use an underscore.Armilla
R
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

Roundish answered 8/10, 2012 at 18:45 Comment(0)
B
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.

Bisson answered 5/3, 2010 at 18:33 Comment(0)
Z
1

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

Zurheide answered 5/3, 2010 at 18:35 Comment(1)
This answer is true but incomplete. There's a workaround. Please see my answer on this question.Lucan
P
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).

Petaliferous answered 5/3, 2010 at 18:53 Comment(5)
The naming convention is from HTML5; dev.w3.org/html5/spec/…Lantha
Ps yeah, \u2014 is like eh not optimal.Lantha
\u002d is the hyphen character. When I saw "dash" in the question, I thought of the "&mdash" character first.Petaliferous
@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).Petaliferous
Nope... new { @data\u002drole = "collapsible"} does not compile.Regolith

© 2022 - 2024 — McMap. All rights reserved.