Word Wrap on Displayfor in MVC

643 views Asked by At

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.

1

There are 1 answers

0
MintBerryCRUNCH On BEST ANSWER

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);
                    }
                }
        }