ojek
ojek

Reputation: 10068

How to get only text from html content inside of a controller?

I have something like this:

using (var context = new MyDb())
{
   var post = context.Posts.Where(p => p.ThreadId == item).OrderBy(p => p.DateCreated).First();
   ViewBag.Description = post.Conent; //post.Content contains content of the post; that means html + user text
}

Currently my ViewBag.Description contains everything that is inside of post.Content, and I need it to contain only text. How do I do it?

Upvotes: 1

Views: 474

Answers (1)

Andrei
Andrei

Reputation: 44600

Use this function to remove HTML tags from a string:

public string StripHtml(string text)
{
    return Regex.Replace(text, "<(.|\\n)*?>", string.Empty);
}

Upvotes: 2

Related Questions