35 lines
1.3 KiB
C#
35 lines
1.3 KiB
C#
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<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> 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));
|
|
}
|
|
}
|
|
}
|