Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static MvcHtmlString GetDisplayValue<TModel>(this HtmlHelper<TModel> htmlHelper, object property)
- {
- var propertyType = property.GetType();
- var maxLengthAttribute =
- propertyType
- .GetCustomAttributes(false)
- .Where(attr => attr.GetType().Equals(typeof(System.ComponentModel.DataAnnotations.MaxLengthAttribute)));
- if (maxLengthAttribute.Count() != 1)
- return new MvcHtmlString("");
- var maxLength =
- maxLengthAttribute
- .Select(attr => ((System.ComponentModel.DataAnnotations.MaxLengthAttribute)attr).Length)
- .Single();
- var propertyValue = property.ToString();
- if (propertyValue.Length > maxLength)
- propertyValue = propertyValue.Substring(0, maxLength - 3) + "...";
- return new MvcHtmlString(propertyValue);
- }
Add Comment
Please, Sign In to add comment