using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace Web.Mvc.ViewHelpers { public static class DisplayExtensions { public static MvcHtmlString DisplayWithIdFor(this HtmlHelper helper, Expression> expression, object htmlAttributes, string wrapperTag = "span") { var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)); if (htmlAttributes != null) { var tag = new TagBuilder(wrapperTag); tag.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) as IDictionary); tag.Attributes.Add("id", id); tag.SetInnerText(HttpUtility.HtmlDecode(helper.DisplayFor(expression).ToString())); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } else { return MvcHtmlString.Create(string.Format("<{0} id=\"{1}\">{2}", wrapperTag, id, HttpUtility.HtmlDecode(helper.DisplayFor(expression).ToString()))); } } } }