I used this article to create an Image helper:
http://www.asp.net/mvc/tutorials/older-versions/views/using-the-tagbuilder-class-to-build-html-helpers-cs. It says it replaces periods, but has no mention of other limitations.
It all works fine unless I try using GUID string as the id.
The Razor code:
@Html.Image(id, ....) //passes a GUID string to helper fine
The ImageHelper is exactly as the webpage describes:
builder.GenerateId(id);
//breakpoint after this shows the builder object
//does not create an id attribute when id is a GUID string.
//using id.ToString() doesn't work for GUIDs either
Using the GUID string in MergeAttributes works fine:
builder.MergeAttribute("title", id); //the GUID string is img title no problem
builder.MergeAttribute("class", id); //the GUID string is class as expected
Is there some limitation or workaround when using GUID strings as an ID?
IDs must start with a letter, not a number. Often, a GUID will start with a number, hence the problem.
TagBuilder.GenerateIdcallsCreateSanitizedId, which performs this check:Your best bet is to just add a prefix, like “guid” or something to the beginning of the ID.