Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created August 16, 2012 19:03
Show Gist options
  • Save jeffjohnson9046/3372680 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/3372680 to your computer and use it in GitHub Desktop.
Describes how the title attribute is added to the option element
internal static string ListItemToOption(ImageSelectListItem item, string imagePath = "")
{
//if (!(item is ImageSelectListItem))
//{
// throw new InvalidCastException(string.Format("Cannot cast {0} to System.Web.Mvc.Html.ImageImageSelectListItem.", item.GetType()));
//}
//ImageSelectListItem imageImageSelectListItem = item as ImageSelectListItem;
TagBuilder builder = new TagBuilder("option")
{
InnerHtml = HttpUtility.HtmlEncode(item.Text)
};
if (item.Value != null)
{
builder.Attributes["value"] = item.Value;
}
if (!string.IsNullOrEmpty(imagePath))
{
builder.Attributes["title"] = BuildFullImagePath(imagePath, item.ImageFileName);
}
if (item.Selected)
{
builder.Attributes["selected"] = "selected";
}
return builder.ToString(TagRenderMode.Normal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment