Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. public static MvcHtmlString GetDisplayValue<TModel>(this HtmlHelper<TModel> htmlHelper, object property)
  2. {
  3. var propertyType = property.GetType();
  4. var maxLengthAttribute =
  5. propertyType
  6. .GetCustomAttributes(false)
  7. .Where(attr => attr.GetType().Equals(typeof(System.ComponentModel.DataAnnotations.MaxLengthAttribute)));
  8.  
  9. if (maxLengthAttribute.Count() != 1)
  10. return new MvcHtmlString("");
  11.  
  12. var maxLength =
  13. maxLengthAttribute
  14. .Select(attr => ((System.ComponentModel.DataAnnotations.MaxLengthAttribute)attr).Length)
  15. .Single();
  16.  
  17. var propertyValue = property.ToString();
  18.  
  19. if (propertyValue.Length > maxLength)
  20. propertyValue = propertyValue.Substring(0, maxLength - 3) + "...";
  21.  
  22. return new MvcHtmlString(propertyValue);
  23. }
Add Comment
Please, Sign In to add comment