开发者

Dash (-) in anonymous class member

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 membe开发者_开发百科r 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.


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;
}


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


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.


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜