I want to remove the html that is stored in my database for Description field. Right now I'm able to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription field but the HTML still shows.
<div>Test<spa ...
I have it set up as this in my model class file:
public string Description { get; set; }
public string ShortDescription => Description?.Length > 16 ? $"{Description.Substring(0, 13)} ..." : Description;
How do I fix this to strip out the html so it says "Test..."
Unclear... My best guess is you are seeing HTML formatted text in the browser window. The rest of my post is related to this assumption.
The MVC framework tries to protect you from malicious scripts by HTML encoding HTML characters. You can easily see this affect when viewing the HTML source in Dev Tools (F12). You'll see encoded characters like < and > of < and >.
If you use Html.Raw(), you'll have invalid HTML. You'll need to come up with a different design. Rather than shorting the string, render the string but use HTML, CSS and perhaps JS to affect the string in the browser. For example, rather than substring,
wrap the remaining string characters in an HTML tag that is not visible on screen. sprinkle in a little JS or CSS to display the three dots.
I would take advantage of HTML and JS to build this feature. Wrap the content coming from the DB in another HTML tag and CSS class that shows 16 charterers.
<div>
<span class="ellipsis">
I want to remove the html that is stored in my database for Description field. Right now I'm able
to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription
field but the HTML still shows
</span>
</div>
Member
197 Points
1144 Posts
Remove HTML using substring
Oct 18, 2019 03:32 PM|bootzilla|LINK
I want to remove the html that is stored in my database for Description field. Right now I'm able to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription field but the HTML still shows.
<div>Test<spa ...
I have it set up as this in my model class file:
How do I fix this to strip out the html so it says "Test..."
All-Star
53711 Points
24040 Posts
Re: Remove HTML using substring
Oct 18, 2019 04:32 PM|mgebhard|LINK
Unclear... My best guess is you are seeing HTML formatted text in the browser window. The rest of my post is related to this assumption.
The MVC framework tries to protect you from malicious scripts by HTML encoding HTML characters. You can easily see this affect when viewing the HTML source in Dev Tools (F12). You'll see encoded characters like < and > of < and >.
The @Html.Raw() method does not encode the string which allows you render raw HTML to the browser; https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#expression-encoding
The problem is your code striping then ending tag from the string.
If you use Html.Raw(), you'll have invalid HTML. You'll need to come up with a different design. Rather than shorting the string, render the string but use HTML, CSS and perhaps JS to affect the string in the browser. For example, rather than substring, wrap the remaining string characters in an HTML tag that is not visible on screen. sprinkle in a little JS or CSS to display the three dots.
Participant
1861 Points
2836 Posts
Re: Remove HTML using substring
Oct 18, 2019 08:32 PM|EnenDaveyBoy|LINK
not tried it but what about:
http://maxprog.net.pl/best-practice/solved-c-how-get-plain-text-from-html-string/
or add aditional fields or information structures to deal with ui requirements.
Member
197 Points
1144 Posts
Re: Remove HTML using substring
Oct 24, 2019 08:46 PM|bootzilla|LINK
Tried this
and it does remove most of html except
How can I get rid of that?
All-Star
53711 Points
24040 Posts
Re: Remove HTML using substring
Oct 24, 2019 08:54 PM|mgebhard|LINK
I would take advantage of HTML and JS to build this feature. Wrap the content coming from the DB in another HTML tag and CSS class that shows 16 charterers.
For Example.
I want to remov...