0

How do i limit the number of characters diplayed.

Here is my code from my cshtml file

@Html.DisplayFor(modelItem => item.Text) 

All Help Appreciated

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Rob177
  • 27
  • 1
  • 9

1 Answers1

4

You can write a helper method like this in the Utility class and import in the Razor page or add in the web.config . You can read here https://msdn.microsoft.com/en-in/library/bb383977.aspx

       public static string DisplayText(this string str , int charallowed){
            if(str.Length > charallowed)
                   return str.Substring(0,charallowed) + " ...." ;
            return str;
        }

        @Html.DisplayFor(modelItem => item.TextDisplayText(20)); 
Devesh
  • 4,500
  • 1
  • 17
  • 28
  • Thanks for the help, do i put the method in the controller and the: @Html.DisplayFor(modelItem => DisplayText(item.Text,20)); in the .cshtml file? @Devesh – Rob177 Jan 25 '15 at 12:40
  • You can create extension for the string class. I have updated the my code. – Devesh Jan 25 '15 at 12:47
  • I like the solution, but it throws an error, same example as someone else posted: http://stackoverflow.com/questions/21754071/templates-can-be-used-only-with-field-access-property-access-single-dimension – CularBytes Apr 10 '16 at 12:10