I am using the following code to display my html encoded text from a database:
@Html.Raw(HttpUtility.HtmlDecode(@item.Content))
What I want to do now is to display for instance only 20 words of the content and and “…” at the end. How should I do this? I was thinking about adding a helper for IHtmlString but I don’t know how to return the x words for IHtmlString
You could write a custom HTML helper that will have the responsibility to parse the input string into constituent words and take the first
xof them:which could be used in your view:
or if you wanted to specify a different number of words to take: