-1

I want to show text on two separate lines for the page header:

@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Message</h1>
            </hgroup>
        </div>
    </section>
}

I have the following code in controller:

public ActionResult Login(string returnUrl)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("Web Security Administration Portal");
    sb.AppendLine();
    sb.AppendLine();
    sb.Append("Services, Inc. - SSOAdmin v2.00");
    ViewBag.ReturnUrl = returnUrl;
    ViewBag.Message = sb.ToString();
    return View();
}

ViewBag does not show new line. When viewing source code of generated html, looks like that two strings are on separated lines, but when page is rendered, lines are not separated.

I tried to use sb.Append(Environment.NewLine) and sb.Append(Text{0}, Environment.NewLIne) as well. Does not help here.

What can be wrong here?

tereško
  • 58,060
  • 25
  • 98
  • 150
gene
  • 2,098
  • 7
  • 40
  • 98
  • 3
    You should read a little more about html before posting this question... – ilans May 08 '15 at 14:33
  • I rolled back your edit, as you shouldn't adjust code in your question that isn't working as you work out your attempt. It will confuse other people trying to answer your question, or people who find your question years from now with a similar problem. – krillgar May 08 '15 at 14:39
  • You should also learn to look at the generated HTML. You would have seen your newline. – John Saunders May 08 '15 at 15:02
  • I looked at generated html and saw the new line but could not understand why `ViewBag` did not generate it. Also, I did not know that I could have used `$Html.Raw`. Also, I did not thing that my problem was related to how html generated new line character. Other posts suggested to use it and nobody mentioned about using `@Html.Raw` with `ViewBag`. – gene May 08 '15 at 16:29

1 Answers1

2

Environment.NewLine puts the text on multiple lines in the HTML that is created. However, white space such as new lines are ignored by most HTML elements. You should add <br /> where you want new lines.

Alternately, you could change that behavior by adding see to the enclosing element that adds:

white-space: pre-wrap;

The best bet to get that to work would be to have your ViewBag be wrapped in a <p>.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • I wrapped `ViewBag` in `

    ` (look at updated code), but everything is still the same

    – gene May 08 '15 at 14:36
  • Did you add that CSS to the `

    ` tag? Ideally, the better solution is to replace your `sb.AppendLine()` with `
    `. That is the normal convention with HTML, and the `white-space` CSS command could get overwritten.

    – krillgar May 08 '15 at 14:37