user3317526
user3317526

Reputation: 91

Carriage returns in html table

I am having problems displaying returns in the html table, it returns as one line.

I am getting the data from an sql 2012 database but it is strange because when I edit the data in a text area it shows the returns.

Any ideas?

 public ActionResult Details(int id = 0)
    {
        WorkoutModel workoutmodel = db.WorkoutModels.Find(id);
        if (workoutmodel == null)
        {
            return HttpNotFound();
        }
        return View(workoutmodel.WorkoutDetails.Replace(System.Environment.NewLine, "<br />"));
    }

Upvotes: 0

Views: 622

Answers (3)

Jerry Joseph
Jerry Joseph

Reputation: 1012

While displaying data from the database you have to replace line breaks with HTML line breaks using the code below.

return View(workoutmodel.WorkoutDetails.Replace("\r\n", "<br />")
                                       .Replace("\n", "<br />")
                                       .Replace("\r", "<br />"));

Upvotes: 1

Jeremy Cook
Jeremy Cook

Reputation: 22153

I go this route because "\n" is part of newline across the major operating systems (Windows \r\n, Mac and *nix \n). It replaces the \n character and cleans up any \r that may be left behind (not that they generally do any harm being left behind in HTML).

valueFromDb.Replace("\n", "<br />").Replace("\r", "");

Upvotes: 0

Doug Morrow
Doug Morrow

Reputation: 1366

Text areas will show returns properly but you will have to parse the returns to breaks in the HTML.

As in:

My text with a \r\nLine break.

Becomes:

My text with a <br />Line break.

Upvotes: 1

Related Questions