Another Word Wrapping question
I have been searching for a solution for the past two days now. I am working with asp.net and certain text from a form gets saved to a database. When pulling the text back from the database I want to display it in a div or label开发者_如何学Python or whatever it really doesn't matter, but I want to recognize the line breaks that were saved and text wrap it in the corner. I thought I had it when I did word-wrap:break-word; and white-space:pre; but then I realized that words got split in two going from one line to the next.
I know that this can be solved with javascript. Is there any way I can do this without having to use javascript or jquery? Any help would be greatly appreciated!
You could write a helper for this:
public static class HtmlExtensions
{
public static IHtmlString FormatText(this HtmlHelper html, string text)
{
if (string.IsNullOrEmpty(text))
{
return MvcHtmlString.Empty;
}
// you could use any line break you might have in your text to split
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
return MvcHtmlString.Create(
string.Join("<br/>", lines.Select(line => html.Encode(line)))
);
}
}
and then:
<div>
@Html.FormatText(Model.SomeText)
</div>
精彩评论