[RESOLVED]Why does my string passed in by Viewbag fail to display new lines?

Hi,

MVC5.  Using this code in a View:

<form action="http://localhost:11704/Home/testpage" method="post">
  <input type="text" name="key1" value="mydata" />
  @Html.Hidden("key2", "value for key2")
  <input type="submit" />
</form>

I POST the 2 name/value pairs above to the ‘testpage’ View and its Controller Action:

@Code
    ViewData("Title") = "testpage"
End Code
<br/>
@ViewBag.message

----------------------------------

  <HttpPost>
  Function testpage(formdata As FormCollection) As ActionResult

    Dim newdata As New StringBuilder()

    For Each dataitem In formdata.Keys
      newdata.Append(dataitem.ToString()).Append(" ")
      newdata.Append(formdata(dataitem.ToString()))
      newdata.AppendLine()
    Next

    ViewBag.message = newdata
    Return View()
  End Function

The testpage View displays the following as a single line:

key1 mydata key2 value for key2 

But the source of the webpage looks like this in the area of the data being displayed (I removed the blank lines between the Viewbag.Message and the footer for viewing):

  <div class="container body-content">
<br/> key1 mydata key2 value for key2 <hr /> <footer> <p>&copy; 2014 - My ASP.NET Application</p> </footer> </div>

I also tested the ‘newdata’ string using the debug output console, and it has the line break.

Why is the testpage view not showing the line break?

Thanks.

Best Regards,

Alan

Well, probably you can try use <br /> for new line.

newdata.Append("<br />")

Hi Siva,

I tried that.  Didn’t work.  It shows up as

key1 mydata - <br /> key2 value for key2 - <br />

I was able to accomplish my goal of listing each name/value pair on a separate line by creating a List(Of String) in the Controller Action and passing it in the View call, and then using the following in the View (VB code is a little different than C#):

@Modeltype List(Of String)
@Code
  ViewData("Title") = "testpage"
End Code

@For Each dstring As String In Model
  @<li>@dstring</li>
Next

But, I would still like to understand the principle I’m missing.  Why can’t I embed something like a vBCrLf, or use the AppendLine() and have that new line show up in the View?

Best Regards,

Alan

Hi Alan,

Could you try using @Html.Raw to treat ViewBag Message as Html – as mentioned in this post:

http://stackoverflow.com/questions/7285790/how-can-i-show-a-viewbag-as-html

Hi Alan,

First, please refer to Siva’s reply to use Html.Raw() and append <br/>

Secondly, you could append n directly.

newdata.Append(dataitem.ToString()).Append("n")
      newdata.Append(formdata(dataitem.ToString()))

Best Regards

Starain

Hi Starain,

Sorry for the delay…

The "n" option didn’t work either.  It just put ‘n’ into the string.

Interestingly, while the html.raw() didn’t work either, a view of the source on the delivered page shows 4 separate lines using the html.raw() method, but incredibly, on the displayed webpage, it’s still all on one line.  Similar to my initial post.

This is so weird.  I’m sure an answer for why the line feeds are not displaying will eventually pop up.  I’m sure it’s something simple. I’ll post on another forum and post back here if the answer shows up.

Best Regards,

Alan

@Html.Raw(ViewBag.message.Replace("rn", "<br/>"))

Edit: note this opens you to cross-side scripting attacks and similar.  Ensure you sanitise where the "message" is coming from also.  You might be better appending a Server.HtmlEncode() copy of your text to the stringbuilder, and rather than .AppendLine
do Append("<br/>") then just do

@Html.Raw(ViewBag.message)

you should learn html. linefeeds are just whitespace. in html all whitespace is covered to a single space. so 10 consecutive linefeeds or any combination of whitespace characters (tab, space, linefeed) are the same as one space. there is a couple exceptions,
the <pre> and <textarea> will honor linefeeds if your css does not override it.

Hi Aidy,

That did not work either.  But thanks for showing a different way to look at the way viewbag can be adjusted on the fly.

Best Regards,

Alan

Hi Bruce,

I guess I’m in the process of learning html, part of that is asking questions I suppose.  I did note that the control characters are whitespace.  But if you take a close look at my very first post in this thread, in the final code section I show what the
source looks like in ‘view source’.  The html delivered to my browser actually has the string displayed on 2 separate lines, but when I view the page in the browser, it’s all on one line.

That’s the part that is confusing for me.  The html seems to be generated correctly, but it doesn’t display as it appears to be generated.  I was assuming this is a viewbag issue, but maybe it’s something else.

I’m positive there is a simple explanation for this; I’m likely too close to the problem to see it.

Anyway, I will continue to learn…

Best Regards,

Alan

Might be a vb.net thing, not sure if it uses  rn as a new line.  You could try

@Html.Raw(ViewBag.message.Replace(System.Environment.NewLine, "<br/>"))

Hi Aidy,

Good job!  Thanks, looking for ‘System.Environment.NewLine’ to replace was the key.  Here’s how it was assembled by asp.net:

key1 value1<br/>key2 value2<br/>key3 value3<br/>key4 value4<br/>

Now that the solution is clear, would you have some idea of what caused this?  I would have assumed the Viewbag would faithfully transmit whatever was placed inside it.  Evidently not…

Anyway, thanks for clearing up the mystery.  Contrary to popular belief, I am learning… :-)

Best Regards,

Alan

Line breaks are a common issue.  When you are in your code behind and tell StringBuilder to "AppendLine" then it appends a Windows new line which is character 10 followed by character 13.  Windows understands the 1013 (CRLF) as "new line" so when you output
that text in something that understands the Windows environment (the output window, a text file, a text box) it knows to do a new line at that point.  StringBuilder and .net are general Windows technologies, not web specific ones so they work in the Windows
way.  However html *doesn’t* understand a "Windows new line" (your website might be viewed on a non-Windows device, it can’t be ruled by Windows standards), it understands its own new line which is "<br>".  So when your text has come from a Windows environment
and you want to show it in an html environment, you need to change CRLF to <br>.  This is just an aspect of developing websites, everyone does this stuff.

Leave a Reply