Word Wrap on Displayfor in MVC

I have a Description field that brings back a large amount of text. When it writes to the page it comes out as one big line. I have tried wrapping it in the below two divs

         <td>
            <div style="word-wrap:break-word;">
                @Html.DisplayFor(modelItem => item.Description)
            </div>
        </td>

and
         <td>
            <div style="overflow:auto;">
                @Html.DisplayFor(modelItem => item.Description)
            </div>
        </td>

The strange thing is they both seem to work for a second when the screen first loads and then it goes back to one line.

I have looked online and ever answer I find says the above should work.

Is anyone aware of something I am missing?

Any help would be greatly appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The links in the comments helped but in the end I had to do a count through the data to add in the br every hundred characters. Then if there where no spaces by the 15th loop it would add one in any way.

 public static MvcHtmlString Display100<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        var model = html.Encode(metadata.Model);
                                                  
        if (String.IsNullOrEmpty(model))
        return MvcHtmlString.Empty;

        if (model.Length > 100)             
        for (int i = 100; i <= model.Length; i++)
        {
                if (model.Length - i > 0)
                {         
                    
                    if (Char.ToString(model[i]) == " ")
                    {
                        model = model.Insert(i, "<br />");
                        i += 100;
                        i = i.Round(100);
                    }
                    if(((double)i/100) % 1 > 0.15)
                    {
                        model = model.Insert(i, "<br />");
                        i += 100;
                        i = i.Round(100);
                    }
                }
        }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x