using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web.Mvc; namespace Kreta.Web.Helpers { public static class DisplayTextExtensions { public static MvcHtmlString KretaDisplayTextFor(this HtmlHelper html, Expression> expression, IDictionary htmlAttributes = null, string id = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); string text = metadata.Model?.ToString(); if (string.IsNullOrWhiteSpace(text)) { return MvcHtmlString.Empty; } var tag = new TagBuilder("label"); tag.MergeAttributes(htmlAttributes); if (!string.IsNullOrWhiteSpace(id)) { tag.MergeAttribute("id", id); } tag.MergeAttribute("displayfor", fullHtmlFieldId); tag.AddCssClass("windowInputValue"); tag.SetInnerText(text); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } } }